Tag: 包装器

从原型改变/包装一个function

我正在写一个框架,使用函数的包装来创build一个debugging工具。 目前,我想通过函数调用来汇报和汇总信息。 我使用下面的代码: function wrap(label, cb) { return function () { report(label); cb.apply(this, arguments); } } 然后为了绑定debugging操作,我将使用: function funcToWrap (){/* Some existing function*/} funcToWrap = wrap("ContextLabel", funcToWrap); 现在,当funcToWrap被调用时,它将通过report()方法连线。 我的要求是现在改变这个语法,以便通过以下方式完成包装: funcToWrap.wrap("ContextLabel"); 理想情况下,这样的事情会解决我的问题,但这当然是非法的: Function.prototype.time = function(label){ var func = this; // The actual difference: this = function () { // ILLEGAL report(label); func.apply(this, arguments); } }; 感谢您对此有所了解。