Question based on this discussion: Adding code to a javascript function programmatically
I read this question in hopes of finding a universal way to append code to a JS function. Unfortunately, the top answer of that question only seems to work with functions that are globally available. I need a way for this to work without a global scope. For example, https://jsfiddle.net/Ylluminarious/rc8smp1z/
function SomeClass () {
this.someFunction = function() {
alert("done");
};
this.someFunction = (function() {
var cached_function = someFunction;
return function() {
alert("new code");
cached_function.apply(this, arguments); // use .apply() to call it
};
}());
}
someInstance = new SomeClass();
someIntance.someFunction();
If you run this code, you'll get an error saying something like: ReferenceError: Can't find variable: someFunction
. I'm not sure why this is happening with this code since I was told that this method of appending code would work in any scope, but that does not seem to be the case, at least with this code. Does anyone know a way to fix this and make the code above work?
EDIT:
Sorry, never mind. There was just a few dumb typos that I made.