Tuesday, August 28, 2012

pointers – Is it possible to have 2 variables pointing to the same object? (javascript)

I\’m trying to have both the variables \”my_a\” and letters.a point to the same object.



//i want letters.a to reference (point to) my_a, not be a copy...
//expected output should be: letters.a = c
//made variables into Objects.. but didn\'t help.

var my_a = new Object(\'a\');
var my_b = new Object(\'b\');
var letters = {\'a\': my_a, \'b\': my_b};

$(\'#output\').append(\'my_a = \' + my_a + \'<br>\');
$(\'#output\').append(\'leters.a = \' + letters.a + \'<br>\');

my_a = new Object(\'c\');

$(\'#output\').append(\'my_a = \' + my_a + \'<br>\');
$(\'#output\').append(\'letters.a = <span style=\"color:red\">\' + letters.a + \'</span>\');



See this fiddle:


http://jsfiddle.net/jCsUq/1/


But as you can see by the output, this is not working.


Any ideas? Is this possible with javascript?


Thank you.






Rating: 1 out of 5 based on 6 ratings



The post pointers – Is it possible to have 2 variables pointing to the same object? (javascript) appeared first on Javascript ASK.






via Javascript ASK http://javascriptask.phpfogapp.com/pointers-is-it-possible-to-have-2-variables-pointing-to-the-same-object-javascript.html

No comments:

Post a Comment