I would like to modifiy a prototyped function of an external library in order to execute some code before that function when it is called. I though to clone that function and then replace it by a new one like so:
Note that I am using a clone function found in another question
This is a simplified example:
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;
};
However, doing so, the oldFunction
seem to lose all its original reference to this
.
Is there a solution?