I'm writing a framework that uses wrapping of functions in order to create a debug tool. Currently, I want to report and aggregate information upon function call. I'm using the following code:
function wrap(label, cb) {
return function () {
report(label);
cb.apply(this, arguments);
}
}
And then in order to bind the debug operation I will use:
function funcToWrap (){/* Some existing function*/}
funcToWrap = wrap("ContextLabel", funcToWrap);
Now, when funcToWrap
is invoked, it is wired to go through report()
method.
The requirement I have is to now change this syntax so that the wrapping is done via:
funcToWrap.wrap("ContextLabel");
Ideally, something like this would solve my issue, but this of course is illegal:
Function.prototype.time = function(label){
var func = this;
// The actual difference:
this = function () { // ILLEGAL
report(label);
func.apply(this, arguments);
}
};
Thank you from ahead for any insight regarding this.