If a variable contains an existing function, how can you replace something inside its code?
It seems toString()
(in order to replace the string) adds an "extra" function() { }
and therefore fails.
For example changing test1 into test2 or test3 in:
var theparent = {
myfunc: function () {
console.log('test1');
}
}
console.log(theparent.myfunc);
theparent.myfunc();
theparent.myfunc = new Function("console.log('test2')"); // Works
console.log(theparent.myfunc);
theparent.myfunc();
theparent.myfunc = new Function(theparent.myfunc.toString().replace('test2', 'test3')); // Adds function() { }
console.log(theparent.myfunc);
theparent.myfunc(); // Fails
Comments
Post a Comment