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'?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
indieman
  • 1,051
  • 2
  • 11
  • 24
  • 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. – lu1s May 11 '12 at 22:10
  • because then every time i edit a property of the object in the array, i have to update the array manually. – indieman May 11 '12 at 22:13
  • Why not edit the array directly when you want to "edit the property of an object in the array"? – jahroy May 11 '12 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... – jahroy May 11 '12 at 22:27

5 Answers5

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'.

hobberwickey
  • 6,118
  • 4
  • 28
  • 29
  • 1
    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. – jahroy May 11 '12 at 23:13
6

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gareth
  • 133,157
  • 36
  • 148
  • 157
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • 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. – jahroy May 11 '12 at 23:54
  • I agree if a is a pointer and you're writing a C program (I guess I've never used pointers in PHP). – jahroy May 12 '12 at 00:02
  • @jahroy: No, but if you set `$b = &$a` then it will be true. – Madara's Ghost May 12 '12 at 05:51
  • Truth - See @hobberwickey's answer [here](http://stackoverflow.com/a/10559622/552792). 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. – jamesmortensen May 12 '12 at 08:25
  • @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. – Felix Kling May 12 '12 at 12:23
  • @FelixKling: Better now? – Madara's Ghost May 12 '12 at 12:26
  • Hey, I was ok with the wording, but yeah, that's definitely clearer :) – Felix Kling May 12 '12 at 12:27
1

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

myArray[0] = "bear";
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dougajmcdonald
  • 19,231
  • 12
  • 56
  • 89
  • 1
    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 http://stackoverflow.com/a/10559622/552792 for more information. – jamesmortensen May 12 '12 at 03:45
  • 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 – dougajmcdonald May 12 '12 at 08:10
  • 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! – jamesmortensen May 12 '12 at 08:20
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; }
  }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jahroy
  • 22,322
  • 9
  • 59
  • 108
  • 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](http://blog.opensourceopportunities.com/2008/07/javascript-and-java-are-pass-by-value.html). Thus, in this specific case, I think your answer and explanation still stand. Consider adding some JavaScript examples as well just to be thorough. :) – jamesmortensen May 12 '12 at 03:51
  • 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). – jahroy May 12 '12 at 04:24
  • 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! – jamesmortensen May 12 '12 at 04:25
  • 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. – jahroy May 12 '12 at 04:28