Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jun 2010
    Posts
    278
    Thanks
    63
    Thanked 8 Times in 8 Posts

    Using references in Javascript.

    Very simple question. How do I obtain the reference to a variable (either scalar or object) in javascript?

    E.g., what is the javascript equivalent of:

    PHP Code:
    $a 1;
    $b =& $a;
    $b 2
    I can't seem to find a straightforward example via Google ... probably not searching for the right thing!

    EDIT: After a little more digging, I found that you can't do it. Seems you have to use an object to wrap the value. (Ugh! Javascript ...)
    Last edited by XmisterIS; 10-25-2013 at 10:15 AM. Reason: Found the answer

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Quote Originally Posted by XmisterIS View Post
    EDIT: After a little more digging, I found that you can't do it. Seems you have to use an object to wrap the value. (Ugh! Javascript ...)
    dig more, all objects are passed by ref, including Number Objects. still, numbers can't change, so there's little advantage to having a number object vs a primitive. You can easily use a getter to do the same thing if you NEED that pattern for some reason:


    Code:
    $a = 1;
    
    Object.defineProperty(self, "$b", {
      get: function(){return $a;},
      set: function(n){return $a=n;}});
    
    $b = 2; 
    
    alert($a); // shows 2
    if you need to do this a lot, i would recommend writing a simple wrapper to Object.defineProperty() to keep your fingers from wearing out...

    this gives you a lot more control over the connection than PHP does, allowing for validation (if(n>0)), transformation(return n*2), or other manipulation instead of just delegation.
    Last edited by rnd me; 10-25-2013 at 05:42 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Yes, but the problem is it does *NOT* allow you to dynamically choose any variable in place of $a.

    In other words, you can't use that to do the equivalent of
    $a = 1;
    $b = & $a;
    $b = 2;
    $c = "framitz";
    $b = & $c;
    $b = "zamboni";

    I can see a couple of ways to achieve that, but both are very ugly.

    You got a good way, RndMe?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Quote Originally Posted by Old Pedant View Post
    Yes, but the problem is it does *NOT* allow you to dynamically choose any variable in place of $a.

    In other words, you can't use that to do the equivalent of
    $a = 1;
    $b = & $a;
    $b = 2;
    $c = "framitz";
    $b = & $c;
    $b = "zamboni";

    I can see a couple of ways to achieve that, but both are very ugly.

    You got a good way, RndMe?
    i don't know what's supposed to happen, is this it? :
    Code:
    function byRef(key, get, set){
      Object.defineProperty(self, key, { get: get, set: set,configurable: true});
     return get();
    };
    
    
    $a = 1;
    $b=byRef("$b", function(){return $a;}, function(n){return $a=n;});
    $c=byRef("$c", function(){return $b;}, function(n){return $b=n;});
    
    $a="yes";
    alert([$a,$b,$c]); // shows "yes,yes,yes"
    
    $b="zamboni";
    alert([$a,$b,$c]); // shows "zamboni,zamboni,zamboni"
    
    $c="cool";
    alert([$a,$b,$c]); // shows "cool,cool,cool"

    or, if we don't mind being lazy (literally), and since we're defining globals anyway, we can use late functions to make a nice compact usage:

    Code:
    function define(key, from){
      function F(s){return Function("n", "return "+s);}
      Object.defineProperty(self, key, { get: F(from), set: F(from+"=n"),configurable: true});
     return self[key];
    };
    
    
    $a="Hello World";
    define("$b", "$a");
    define("$c", "$b");
    define("$d", "$b.bold()"); //NICE!
    
    $a="yes";
    alert([$a,$b,$c, $d]); // shows "yes,yes,yes,<b>yes</b>"
    Last edited by rnd me; 10-25-2013 at 09:02 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • Users who have thanked rnd me for this post:

    Old Pedant (10-25-2013)

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Yes, that second scheme was what I was looking for. I think that's as close to the PHP stuff as you will get. Nice.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Consider this code. Works just like the equivalent would in PHP:
    Code:
    function define(key, from){
      function F(s){return Function("n", "return "+s);}
      Object.defineProperty(self, key, { get: F(from), set: F(from+"=n"),configurable: true});
     return self[key];
    };
    
    
    $a="Hello World";
    define("$b", "$a");
    $a = "yes";
    document.write("$b:" + $b + "<hr>");
    
    $c = 0;
    define("$b", "$c");
    $c = 3.14159;
    document.write("$b:" + $b + "<hr>");
    
    define("$c", "$a");
    $c = "yikes";
    document.write("$a:" + $a + "<hr>");
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •