4

Possible Duplicate:
How can I store reference to a variable within an array?

Consider the following code:

var a = 'cat';
var b = 'elephant';
var myArray = [a,b];

a = 'bear';

myArray[0] will still return 'cat'. Is there a way to store references in the array instead of clones, so that myArray[0] will return 'bear'?

4
  • No, but in this case you may want to update the array: myArray[0]='bear'. I can't find a reason why to create such reference. Commented May 11, 2012 at 22:10
  • because then every time i edit a property of the object in the array, i have to update the array manually. Commented May 11, 2012 at 22:13
  • Why not edit the array directly when you want to "edit the property of an object in the array"? Commented May 11, 2012 at 22:19
  • 2
    You're changing WHICH object the variable a refers to. You're not changing the object THAT variable a refers to, so it wouldn't work anyways... Commented May 11, 2012 at 22:27

5 Answers 5

7

While I agree with everyone else saying that you should just use myArray[0] = whatever, if you really want to accomplish what you're trying to accomplish you could make sure that all you variables in the array are objects.

var a = {animal: 'cat'},
    b = {animal: 'elephant'};

var myArray = [a, b];

a.animal = 'bear';

myArray[0].animal is now 'bear'.

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

1 Comment

Exactly. However if you create a new object, then assign that to a (a = newObject in stead of a.animal = newObject), it will not work. This is where I think the OP (or maybe me) is confused.
6

No. JavaScript doesn't do references in that way.

Comments

1

Nope, it is not possible. JavaScript doesn't support such references.

Only objects are stored as references. But I doubt it's what you want here.

7 Comments

So you're saying if I set $a = 'foo', then $b = $a, $c = $a, and $a = 'junk', you're suggesting that $b and $c will now be equal to 'junk'?? I don't have a php environment handy, but that seems pretty crazy.
I agree if a is a pointer and you're writing a C program (I guess I've never used pointers in PHP).
@jahroy: No, but if you set $b = &$a then it will be true.
Truth - See @hobberwickey's answer here. I'm not sure I agree that JavaScript doesn't support references. When you assign an object to an array, you can modify the property and it will be modified in the array as well. Is there perhaps something I'm missing? Keep in mind the question is about JavaScript.
@jmort253: I agree that Truth's answer is not very specific in this point. I assumed by "such references" he meant "references to primitive values" which really don't exist in JavaScript (or other languages, such as Python, Java,...). Of course reference to objects is a different topic.
|
1

You've kind of answered your own question. If you want myArray[0] to equal bear, then set:

myArray[0] = "bear";

3 Comments

You may have missed the point of the question. The op was asking if he could store references in the array, precisely so that he wouldn't have to explicitly access the array directly like you have. See stackoverflow.com/a/10559622/552792 for more information.
I'm aware of that, purely indicating that if the goal is to change the value of something stored in the array that it would need to be accessed via the array indexer
In general, on StackOverflow, answers should answer the question. Otherwise, they should be posted as comments on the question. Consider adding more information that answers the question as that would make your post more valuable to the community. Good luck!
1

Even if your array holds references to objects, making a variable refer to an entirely different object would not change the contents of the array.

Your code does not modify the object variable a refers to. It makes variable a refer to a different object altogether.

Just like your JavaScript code, the following Java code won't work because, like JavaScript, Java passes references to objects by value:

  Integer intOne = new Integer(1);
  Integer intTwo = new Integer(2);

  Integer[] intArray = new Integer[2];
  intArray[0] = intOne;
  intArray[1] = intTwo;

  /* Make intTwo refer to a completely new object */

  intTwo = new Integer(45);

  System.out.println(intArray[1]);

  /* output = 2 */

In Java, if you change the object referenced by a variable (instead of assigning a new reference to a variable) you get the behavior you desire.

Example:

  Thing thingOne = new Thing("funky");
  Thing thingTwo = new Thing("junky");

  Thing[] thingArray = new Thing [2];

  thingArray[0] = thingOne;
  thingArray[1] = thingTwo;

  /* Modify the object referenced by thingTwo */

  thingTwo.setName("Yippee");

  System.out.println(thingArray[1].getName());

  /* output = Yippee */

  class Thing
  {
      public Thing(String n) { name = n; }
      private String name;
      public String getName() { return name; }
      public void setName(String s) { name = s; }
  }

4 Comments

I know you put a lot of work into this, and it's really well written, but the question was about JavaScript, and the question was tagged JavaScript not Java. However, Java and JavaScript are both pass by value where object references are also passed by value. Thus, in this specific case, I think your answer and explanation still stand. Consider adding some JavaScript examples as well just to be thorough. :)
Thanks. I realize that the question was about JavaScript, but figured this might help to illustrate a point. You make a good point that it would be pretty simple to add some javascript (or replace the java).
I'm in favor of adding. I don't like throwing out good information, even if it may be slightly misplaced. One small point, be sure to check out the article on Java and JavaScript being pass by value not pass by reference. In your answer, that's the only thing you might need to fix. :) Good luck!
Thanks, I just read it... and will fix. I guess I was one of those people who thought java passed objects by reference rather than passing those references by value. Yay, I learned stuff totday! Thanks again.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.