I want to decorate a function of a JavaScript "class" (prototype):
SomeClass = function() { this.someVar = 5; };
SomeClass.prototype = {
foo: function(arg) { /* do something with this.someVar */ }
}
However, I cannot change the source of SomeClass nor am I able to influence the instance creation of SomeClass instances.
Therefore I thought about doing the following:
var oldFoo = SomeClass.prototype.foo;
SomeClass.prototype.foo = function(arg) {
console.log("Yey, decorating!");
oldFoo(arg);
};
This seems to work fine, however, due to function scoping oldFoo cannot access someVar anymore (the this object is now window). How to overcome this issue?