Override referenced functions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • limweizhong
    New Member
    • Dec 2006
    • 62

    Override referenced functions

    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.
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

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

    Code:
    var x,o1,o2;
    x=function(){ alert(this.z); };
    o1={z:"o1",t:"o1t"};
    o1.alertfunc=x;
    o2={z:"o2",t:"o2t"};
    o2.alertfunc=x;
    o1.alertfunc(); // alerts "o1"
    o2.alertfunc(); // alerts "o2"
    
    // do you mean that?
    x=function(){ alert(this.t); };
    o1.alertfunc(); 
    
    // --> outputs 'o1' - functions aren't passed by reference
    
    // so you would need to reassign the func to o1 like:
    o1.alertfunc = x;
    o1.alertfunc();
    
    // --> now it outputs 'o1t' - which is a quite, hmmm,
    // 'ugly' way of doing such things :)
    so you would have to use a bit OO magic :) like this:

    Code:
    var x,o1,o2;
    
    // we use an obj and its prototype to declare a func
    function baseObj() {}
    
    baseObj.prototype.x = function() {
        alert(this.z);
    };
    
    o1 = {
        z: "o1",
        t: "o1t"
    };
    
    // we assign the function and bind the scope 
    o1.alertfunc = function() {
        baseObj.prototype.x.apply(this, arguments);
    }
    
    o1.alertfunc(); // --> outputs 'o1'
    
    // we modify our basObj's prototype 
    baseObj.prototype.x = function() {
        alert(this.t);
    };
    
    o1.alertfunc(); // --> now output is: 'o1t'
    kind regards
    Last edited by gits; Oct 5 '11, 12:50 PM.

    Comment

    • limweizhong
      New Member
      • Dec 2006
      • 62

      #3
      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.protot ype.x" with "myFunctionName " and it would also work?

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5388

        #4
        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:

        Code:
        function baseObj(params) {
            for (var i in params) {
                this[i] = params[i];
            }
        }
        
        baseObj.prototype.alertfunc = function(p) {
            alert(this[p]);
        };
        
        var o = new baseObj({z: 'o1', t: 'o1t'});
        
        o.alertfunc('z');
        o.alertfunc('t');
        there is only one func and you could easyly extend it etc.

        kind regards
        Last edited by gits; Oct 6 '11, 10:31 AM.

        Comment

        Working...