Basically i need to overwrite the my.var variable with something else before my.function() is run. Is there a way to do this?
var oldfunction = my.function;
my.function = function () {
my.var = "whatever you want";
oldfunction.apply(this, arguments);
}
This is, of course, if my
or my.function
isn't overwritten by the code you can't modify directly before the call to my.function
(as my.var
is).
For example, in the following scenario:
///your code goes here
///code you cannot modify below
var my = {};
my.function = something;
my.var = 'cake';
my.function();
what you want is impossible (unless you're able to redefine something
or something in it in the same way).
Basically, in this case the only thing you could do is to write (let's assume something
is function () { alert(my.var) }
)
var oldalert = window.alert;
window.alert = function (message) {
oldalert(message === 'cake' ? 'whatever you want' : message);
}
Well, you've got an idea.
Injecting some code in between my.var = 'cake';
and my.function();
is, from the other side, imposible. Roughly speaking, you can choose between two options, whether your code will be executed before my.var = 'cake';
or after my.function();
. Executing your code aftre my.var = 'cake';
but before my.function();
is impossible (if we're speaking of a production environment; of course you could do anything by hands using the debugger, if you need to modify my.var
for a debugging purpose).