1

I'm sure I'm missing something obvious here, but I'd expect the argument to the changeMe method to be passed "by reference" - in other words, that changes to the parameter inside the function will change the variable outside the function.

The following was run in jsfiddle, using Chrome's F12 developer tools to show the console output. http://jsfiddle.net/fzEpa/

var object1 = { Property1: 'Value1' };
changeMe(object1);
console.log(object1);

function changeMe(refToObject) {
    console.log(refToObject);
    refToObject = { Property1: 'Value2' };
    console.log(refToObject);
}
Greg Woods
  • 2,697
  • 2
  • 26
  • 18
  • 1
    See [Is JavaScript a pass-by-reference or pass-by-value language?](http://stackoverflow.com/q/518000/897024) – kapex Apr 17 '13 at 12:57
  • 1
    In the @kapep link, I was confused by "the item passed in is passed by value. But the item that is passed by value is itself a reference." - until I saw it apply to my own code. Now it makes sense – Greg Woods Apr 17 '13 at 14:03

4 Answers4

11

It is passed by reference, but it is a reference to the object, not to the object1 variable (which is also a reference to the object).

You are overwriting the reference to the object with a reference to a new object.

This leaves the original reference to the original object intact.

To modify the object, you would do something like this:

function changeMe(refToObject) {
    refToObject.Property1 = 'Value2';
}
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
3

A reference to the object is passed as the argument value. However:

refToObject = { Property1: 'Value2' };

At this point you lose the reference to the object formerly referenced by refToObject as you're assigning this variable to reference a different object.

Now if you were to edit the refToObject's properties instead of discarding the former object reference, your code would work as exepected (as firstly explained in @Quentin's answer).

Fabrício Matté
  • 69,329
  • 26
  • 129
  • 166
  • almost a tie with @Quentin. Clear explanation - though reading around, it seems some like to take offense at the statement "It is passed by reference" - From what I now understand, it seems to be a reference which is passed by value (if truly passed by reference, I think my code should have worked) – Greg Woods Apr 17 '13 at 14:06
  • @GregWoods Wording isn't my strongest point, but you're right, in other languages such as PHP that would work fine when truly passed as reference ([demo](http://codepad.viper-7.com/El977T)), a better wording would be: "a reference to the object is passed as the argument value", fixed that. `=]` – Fabrício Matté Apr 17 '13 at 15:08
0

If you're familiar with C++ this would be equal to doing something like this:

void f(int* ref) {
    ref = new int(3);
}
int* a = new int(5);
f(a);
printf("%d",a); //Prints 5
Tomer Arazy
  • 1,833
  • 10
  • 15
0

you are trying to redefine the Property 1,so it wouldnt work. in order to work with pass by reference ypu have to do it this way refToObject.Property1='Value2' inside your ChangeMe() function . refer this for better understanding Pass Variables by Reference in Javascript

Community
  • 1
  • 1
dreamweiver
  • 6,002
  • 2
  • 24
  • 39