Skip to content

Instantly share code, notes, and snippets.

@mattsnider mattsnider/annotation.js
Last active Oct 8, 2017

Embed
What would you like to do?
JavaScript function for annotating other JavaScript
function annotate(fnToAnnotate) {
// already annotation aware, use the original annotation chain
if (fnToAnnotate.by) {
return fnToAnnotate;
}
var aAnnotationChain = [fnToAnnotate];
function applyChainFunctions(fn) {
fn.by = function(fnAnnotation, arg1, /*...*/ argN) {
var fnToAnnotate = aAnnotationChain[aAnnotationChain.length - 1],
fnExecutedAnnotation = function () {
/*
Replace the annotation function in the args list,
with the function to annotate. This is a nice little hack
to allow us to call the annotation function using apply, passing
the function to annotate as the first argument, followed by
all the other arguments that were originally passed to ``by``.
*/
arguments[0] = fnToAnnotate;
return fnAnnotation.apply(this, arguments);
}.apply(this, arguments);
aAnnotationChain.push(fnExecutedAnnotation);
return applyChainFunctions(fnExecutedAnnotation);
};
return fn;
}
return applyChainFunctions(fnToAnnotate);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.