10

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.

3
  • Well, you're not really doing what you think your are. You are pointing one object's inner variable at another object's value. To do what you are trying to do though is just silly in JavaScript. May I ask what your end goal is? Perhaps we can find a better solution. Commented Aug 27, 2012 at 14:24
  • @SpYk3HH I just need a reference to the current "letter". There are only two "letters". Old or new. They need to be switched at different times. letter1 becomes old, letter2 becomes new. Commented Aug 27, 2012 at 14:29
  • @Wesley I updated my answer with a possible approach, depending on your browser support needs. Commented Aug 27, 2012 at 14:35

6 Answers 6

6

The answer to your question is "yes", but I think that your question doesn't describe accurately the thing you're trying to do. What you'd like is for letters.a to be reference to the variable "my_a", in the sense of what one can do in C++ with the & operator. That's not possible in JavaScript.

At the statement:

my_a = new Object('c');

you're giving "my_a" a new, different value. Thus, letters.a still refers to the same thing it did, while "my_a" has changed. There's no way to make a variable or object property "track" another (in JavaScript).

edit — actually it occurs to me that you could do something like what you're looking for by defining a "getter" for the "a" property of "letters", one that returns the current value of "my_a". It'd require that feature in the JavaScript engine you're using, but it'd look something like:

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

IE before IE9 doesn't support that unfortunately. Here's the updated jsfiddle.

Sign up to request clarification or add additional context in comments.

1 Comment

One other thing you could do would be a to make letters an Array and write a simple func to FiFo the array with new characters. Something like function fifoArray(myArray, newValue) { myArray.pop(); myArray.unshift(newValue); } Then myArray[0] would always be the newest and myArray[1] would always be the last
4

You can point 2 variables to the same object or value in javascript like so:

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

Also, there is no need to use the object constructor, curly braces serve as the constructor and save typing. Ie, this:

var a = new Object();

Is equivalent to:

var a = {}

1 Comment

Are there any improvements or benefits to doing this?
1

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 the 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 a new object. But letters.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 letters.a pointing to the object 'a'.

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

letters.a = my_a

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

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

Comments

0

Yes, it's 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 afterwards did not change letters, as it already contains a direct pointer to Object('a').

Comments

0

Yes, it is very much possible and is the default. However, in your code you are modifying the reference (my_a = new Object('c');) when you mean to be modifying the value. This would be more clearly illustrated if you used arrays instead of strings, e.g. my_1 = [23]; my_1[0] = 8;

Comments

0

Why you aren't using such a construct:

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

It's much more cleaner imho.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.