Is it possible to have 2 variables pointing to the same object? (Javascript)

I am trying to have both the variables "my_a" and the letters. Points 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 script:

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

But, as you can see from the result, this does not work.

Any ideas? Is this possible with javascript?

Thanks.

+8
javascript pass-by-reference pointers
source share
6 answers

The answer to your question is yes, but I think your question does not describe exactly what you are trying to do. You want letters.a refer to the my_a variable in the sense of what you can do in C ++ using the & operator. This is not possible in JavaScript.

In a statement:

 my_a = new Object('c'); 

you give "my_a" a new, different value. Thus, letters.a still refers to the same as it, and "my_a" has changed. There is no way to make the property of a variable or object a β€œtrack” different (in JavaScript).

edit - it actually occurs to me that you can do something like what you are looking for by specifying "getter" for the property "a" of the "letter", which returns the current value of "my_a". To do this, you need this function in your JavaScript engine, but it looks something like this:

 var letters = {}; Object.defineProperty(letters, "a", { get: function() { return my_a; }, set: function(v) { my_a = v; } }); 

IE before IE9 does not support this, unfortunately. Updated jsfiddle here.

+5
source share

You can specify 2 variables for the same object or value in javascript as follows:

 var a = b = {}; // both a and b now reference the same object 

In addition, there is no need to use the constructor of the object, curly braces serve as a constructor and save text input. Those. this is:

 var a = new Object(); 

It is equivalent to:

 var a = {} 
+3
source share

You are confused by the concept of pointers. When you store an object in a variable, you actually store the address of that object in a variable.

So my_a and letters.a both contain the address of the same object. then you change my_a to contain the address of the new object. But letter.a still contains the address of the first object!

So, at the end of your script, you have my_a pointing to the object 'c', and the letters .a point to the object 'a'.

The only way to get the letters .a and my_a to point to the object you call 'c' is to do

 letters.a = my_a 

again by setting the letters .a to contain the address of the new 'c' object.

By the way, variables with a more basic type (for example, ints, char) actually contain the value of the variable itself, and not a pointer to the actual data.

+2
source share

Yes it is possible.

You were in the right direction, but made a small mistake. When you initialized var letters = {'a': my_a, 'b': my_b}; , you inserted the object that my_a pointed to at that moment .

Changing my_a did not subsequently change letters , since it already contains a direct pointer to Object('a') .

0
source share

Yes, this is very possible by default. However, in the code, you change the link (my_a = new Object ('c');) when you want to change the value. This will be more clearly illustrated if you used arrays instead of strings, for example. my_1 = [23]; my_1 [0] = 8;

0
source share

Why do not you use such a construction:

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

This is a much cleaner IMHO.

0
source share

All Articles