Hi,
Say I have already executed the following code in a JavaScript interpreter:
[code=javascript]
var x,o1,o2;
x=function(){ alert(this.z); };
o1={z:"o1",t:"o 1t"};
o1.alertfunc=x;
o2={z:"o2",t:"o 2t"};
o2.alertfunc=x;
o1.alertfunc(); // alerts "o1"
o2.alertfunc(); // alerts "o2"[/code]
How do I change the function referenced by x, o1.alertfunc and o2.alertfunc to [code=javascript]function(){ alert(this.t); }[/code] without doing the reassignments all over again?
I know a workaround would be to set x to call another function which I can then modify when necessary, but thought that there might be a better way.
Say I have already executed the following code in a JavaScript interpreter:
[code=javascript]
var x,o1,o2;
x=function(){ alert(this.z); };
o1={z:"o1",t:"o 1t"};
o1.alertfunc=x;
o2={z:"o2",t:"o 2t"};
o2.alertfunc=x;
o1.alertfunc(); // alerts "o1"
o2.alertfunc(); // alerts "o2"[/code]
How do I change the function referenced by x, o1.alertfunc and o2.alertfunc to [code=javascript]function(){ alert(this.t); }[/code] without doing the reassignments all over again?
I know a workaround would be to set x to call another function which I can then modify when necessary, but thought that there might be a better way.
Comment