0

Consider this chunk of code, which is a basic example of passing an argument by value (here it's an object, which is passed by value):

function setName(obj) {
 obj.name = "Pork";
 obj = new Object();
 obj.name = "Chicken";
}

var person = new Object();
setName(person);
alert(person.name);

The output of this code is "Pork" (obviously), but what I'm trying to understand is why the new object created in the setName function does not overwrite the value stored in obj. Instead, this apparently creates a pointer to a local object, which is destroyed when the function execution ends.

dolanator
  • 180
  • 3
  • 13
  • You would like to read the [answer](http://stackoverflow.com/a/3638034/1960455) to the question [Is JavaScript a pass-by-reference or pass-by-value language?](https://stackoverflow.com/questions/518000). – t.niese Mar 16 '15 at 16:44
  • 1
    possible duplicate of [Is JavaScript a pass-by-reference or pass-by-value language?](http://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language) – Matteo Tassinari Mar 16 '15 at 16:45
  • Thanks, I will read all the answers, and see if they clarify what I was asking. – dolanator Mar 16 '15 at 16:50
  • There is [this](http://architects.dzone.com/articles/java-pass-value-and-not-pass-0) very good explanation with diagrams of object references and their values. Its for java though, but in this case they behave similar. – Vishwanath Mar 16 '15 at 16:57
  • Btw, to everyone who downvoted this question: I did state I searched an answer and didn't find one in the searches (someone edited that from the question). But you have to take into account a search for keywords on this issue returns hundreds of pages, so it's possible I missed the right answer. Not my fault the search function does not order results by relevance right. – dolanator Mar 16 '15 at 18:05

5 Answers5

4

Objects are passed as reference in JavaScript, but the reference itself is a value. In other words, you can only change the members of the input object, but not its reference. What happens in the line

obj = new Object();

is that the local copy of the reference to "obj" is modified, but this local copy is discarded when the function returns.

Shanqing Cai
  • 3,756
  • 3
  • 23
  • 36
  • I guess it would help if these books which teach JS stated more clearly that primitives are passed by value (as a copy) and objects are passed by value as a reference (basically a pointer, in C++ terms). – dolanator Mar 16 '15 at 17:24
3

obj, even though it is the name of the argument, behaves like a local variable within the setName function. When the function is called, it refers to whatever was passed in as the argument. When you set it equal to a new, different object, it's behaving as a local reference, now pointing to the (new) object. At that point, you've lost the reference to the original argument. (And when the function ends, the reference to the newer object is itself lost.)

function setName(obj) { // "obj" is the local variable that will refer to the argument
 obj.name = "Pork";  // obj points to the object passed in, which is modified
 obj = new Object(); // obj is set to point to a new Object
 obj.name = "Chicken";  // the new object is modified.
     // the new object is dropped here 
}
Ben Zotto
  • 70,108
  • 23
  • 141
  • 204
0

Every time you use this: new Object() you are creating a new instance of the object. If you want to change current information you would need to stick with the same variable without creating a new Object() variable.

CodeGodie
  • 12,116
  • 6
  • 37
  • 66
0

Your analysis appears to be correct. Note that in your existing function you were passing a reference to the object created before it was called. You then overwrote the reference with a reference to the new object.

If you had wanted to construct a new value you would either need to make it the return value of the function or set the new object as an attribute of an argument or as a value in an array.

Alex Fitzpatrick
  • 643
  • 5
  • 10
0

when you are calling setName function ,you are passing reference of person to it.now both obj and person holds reference to the object. obj doesn't hold person object(neither does person).both person and obj shares an object.when you create a new object new Object() ,you assign the reference to that object in obj.so now obj holds reference to completely different object.thats why you cant mutate person object anymore.

balajisoundar
  • 581
  • 2
  • 11