469,294 Members | 1,730 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,294 developers. It's quick & easy.

Override referenced functions

Hi,

Say I have already executed the following code in a JavaScript interpreter:
Expand|Select|Wrap|Line Numbers
  1. var x,o1,o2;
  2. x=function(){ alert(this.z); };
  3. o1={z:"o1",t:"o1t"};
  4. o1.alertfunc=x;
  5. o2={z:"o2",t:"o2t"};
  6. o2.alertfunc=x;
  7. o1.alertfunc(); // alerts "o1"
  8. o2.alertfunc(); // alerts "o2"
How do I change the function referenced by x, o1.alertfunc and o2.alertfunc to
Expand|Select|Wrap|Line Numbers
  1. function(){ alert(this.t); }
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.
Sep 30 '11 #1

✓ answered by gits

what do you mean exactly with your workaround? the following will always output 'o1':

Expand|Select|Wrap|Line Numbers
  1. var x,o1,o2;
  2. x=function(){ alert(this.z); };
  3. o1={z:"o1",t:"o1t"};
  4. o1.alertfunc=x;
  5. o2={z:"o2",t:"o2t"};
  6. o2.alertfunc=x;
  7. o1.alertfunc(); // alerts "o1"
  8. o2.alertfunc(); // alerts "o2"
  9.  
  10. // do you mean that?
  11. x=function(){ alert(this.t); };
  12. o1.alertfunc(); 
  13.  
  14. // --> outputs 'o1' - functions aren't passed by reference
  15.  
  16. // so you would need to reassign the func to o1 like:
  17. o1.alertfunc = x;
  18. o1.alertfunc();
  19.  
  20. // --> now it outputs 'o1t' - which is a quite, hmmm,
  21. // 'ugly' way of doing such things :)
  22.  
so you would have to use a bit OO magic :) like this:

Expand|Select|Wrap|Line Numbers
  1. var x,o1,o2;
  2.  
  3. // we use an obj and its prototype to declare a func
  4. function baseObj() {}
  5.  
  6. baseObj.prototype.x = function() {
  7.     alert(this.z);
  8. };
  9.  
  10. o1 = {
  11.     z: "o1",
  12.     t: "o1t"
  13. };
  14.  
  15. // we assign the function and bind the scope 
  16. o1.alertfunc = function() {
  17.     baseObj.prototype.x.apply(this, arguments);
  18. }
  19.  
  20. o1.alertfunc(); // --> outputs 'o1'
  21.  
  22. // we modify our basObj's prototype 
  23. baseObj.prototype.x = function() {
  24.     alert(this.t);
  25. };
  26.  
  27. o1.alertfunc(); // --> now output is: 'o1t'
  28.  
kind regards

3 992
gits
5,390 Expert Mod 4TB
what do you mean exactly with your workaround? the following will always output 'o1':

Expand|Select|Wrap|Line Numbers
  1. var x,o1,o2;
  2. x=function(){ alert(this.z); };
  3. o1={z:"o1",t:"o1t"};
  4. o1.alertfunc=x;
  5. o2={z:"o2",t:"o2t"};
  6. o2.alertfunc=x;
  7. o1.alertfunc(); // alerts "o1"
  8. o2.alertfunc(); // alerts "o2"
  9.  
  10. // do you mean that?
  11. x=function(){ alert(this.t); };
  12. o1.alertfunc(); 
  13.  
  14. // --> outputs 'o1' - functions aren't passed by reference
  15.  
  16. // so you would need to reassign the func to o1 like:
  17. o1.alertfunc = x;
  18. o1.alertfunc();
  19.  
  20. // --> now it outputs 'o1t' - which is a quite, hmmm,
  21. // 'ugly' way of doing such things :)
  22.  
so you would have to use a bit OO magic :) like this:

Expand|Select|Wrap|Line Numbers
  1. var x,o1,o2;
  2.  
  3. // we use an obj and its prototype to declare a func
  4. function baseObj() {}
  5.  
  6. baseObj.prototype.x = function() {
  7.     alert(this.z);
  8. };
  9.  
  10. o1 = {
  11.     z: "o1",
  12.     t: "o1t"
  13. };
  14.  
  15. // we assign the function and bind the scope 
  16. o1.alertfunc = function() {
  17.     baseObj.prototype.x.apply(this, arguments);
  18. }
  19.  
  20. o1.alertfunc(); // --> outputs 'o1'
  21.  
  22. // we modify our basObj's prototype 
  23. baseObj.prototype.x = function() {
  24.     alert(this.t);
  25. };
  26.  
  27. o1.alertfunc(); // --> now output is: 'o1t'
  28.  
kind regards
Oct 5 '11 #2
Yes, I actually meant something like that but didn't realise you would need to use "apply" to make it work. Thanks.

So I assume creating new functions is the only way to pass functions by reference? Wouldn't that be quite inefficient...?

Also, why do you need to use an object? I thought you could just replace "baseObj.prototype.x" with "myFunctionName" and it would also work?
Oct 5 '11 #3
gits
5,390 Expert Mod 4TB
by using an object you might use much better code then the shown one - i used the obj above to give you some idea of it. if i had to realise something like your example i would use code like the below:

Expand|Select|Wrap|Line Numbers
  1. function baseObj(params) {
  2.     for (var i in params) {
  3.         this[i] = params[i];
  4.     }
  5. }
  6.  
  7. baseObj.prototype.alertfunc = function(p) {
  8.     alert(this[p]);
  9. };
  10.  
  11. var o = new baseObj({z: 'o1', t: 'o1t'});
  12.  
  13. o.alertfunc('z');
  14. o.alertfunc('t');
  15.  
there is only one func and you could easyly extend it etc.

kind regards
Oct 5 '11 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by | last post: by
15 posts views Thread by Jerry He | last post: by
39 posts views Thread by Geronimo W. Christ Esq | last post: by
7 posts views Thread by Bob Stearns | last post: by
7 posts views Thread by Nick Keighley | last post: by
14 posts views Thread by v4vijayakumar | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.