我想修改外部库的原型函数,以便在调用函数之前执行某些代码。我虽然克隆该功能,然后通过一个新的像这样替换它:Javascript - 克隆原型函数
请注意,我用another question
发现克隆功能这是一个简单的例子:
var oldFunction = anObject.aFunction.clone();
anObject.aFunction = function(a, b, c) {
if (a > b) {
return;
} else {
oldFunction(a, b, c);
}
}
Function.prototype.clone = function() {
var that = this;
var temp = function temporary() { return that.apply(this, arguments); };
for(var key in this) {
if (this.hasOwnProperty(key)) {
temp[key] = this[key];
}
}
return temp;
};
但是,这样做,oldFunction
似乎失去了其原始参考this
。
有没有解决方案?