Antonio Pavicevac-Ortiz
Antonio Pavicevac-Ortiz

Reputation: 7739

In Javascript, how is this argument passed by value and not by reference?

I am trying to wrap my head around this idea of 'argument passing.' In a book I am reading it states that arguments are only passed by value not by reference.

function addTen(num) { 
   num + = 10;
   return num; 
} 


var count = 20; 
var result = addTen(count); 
alert(count); // 20 - no change 
alert(result); // 30

The example above is pretty clear, but the example below leaves me very confused.

When person is passed to the setName function, doesn't it mirror the local variable 'obj' and flow down the statements in the function? i.e. person is first set to the property name, then it's assigned to a new Object, and finally this new created person object is assigned the property 'Gregg'????

Why do you get 'Nicholas'!!!!

function setName(obj) {
    obj.name = "Nicholas"; 
    obj = new Object(); 
    obj.name = "Greg"; 
}        

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

Upvotes: 3

Views: 215

Answers (5)

Teemu
Teemu

Reputation: 23406

Objects are passed to function as a copy of the reference. Now what happens in your example is next:

var person = new Object();     

function setName(obj) { // a local obj* is created, it contains a copy of the reference to the original person object
    obj.name = "Nicholas"; // creates a new property to the original obj, since obj here has a reference to the original obj
    obj = new Object(); // assigns a new object to the local obj, obj is not referring to the original obj anymore
    obj.name = "Greg"; // creates a new property to the local obj
}

setName(person);
alert( person.name); //" Nicholas"

* = obj is a local variable containing a value, which is a reference to the original obj. When you later change the value of the local variable, it's not reflecting to the original object.

Upvotes: 3

Guffa
Guffa

Reputation: 700362

Parameters are passed by value, and for objects that means that the value is a copy of the reference to the object.

It's the same thing as when you do a simple assignment, that is also by value:

// a variable that contains a reference to an object:
var person = new Object();
// another variable, that gets a reference to the same object:
var people = person;
// a function call with a reference to the object:
setName(person);

In the function, the parameter is a local variable that works independently of the variable used to send the reference into the function, but it references the same object:

function setName(obj) {
  // as the variable references the original object, it changes the object:
  obj.name = "Nicholas";
  // now the variable gets a reference to a new object
  obj = new Object();
  // the new object is changed
  obj.name = "Greg";
}

Upvotes: 1

mohkhan
mohkhan

Reputation: 12315

Everything is pass-by-value in JavaScript. When you pass anything as an argument, JS makes a copy of that and sends it to the function. When an Object is passed, JS copies the reference of that object into another variable and passes that copied variable to the function. Now, since the passed variable is still pointing to the object, you can alter its properties using the . or [] notation. But if you use new to define a new object then that variable just points to the new reference.

function setName(obj) {
    obj.name = "Nicholas"; // obj pointing to person reference
    obj = new Object();  // obj now pointing to another reference
    obj.name = "Greg"; // you have changed the new reference not person reference
}        

var person = new Object(); // person pointing to person reference
setName(person); 
alert( person.name); //" Nicholas"

Upvotes: 2

Esailija
Esailija

Reputation: 140230

Pass by reference means this:

function a(obj) {
    obj = 3;
}

var test = new Object();
a(test);
console.log(test); //3, so I have lost the object forever

I.E. even variable assignments will be visible to the caller.

Of course if the target function modifies an object passed to it then those modifications to the object itself will be visible to the caller.

Upvotes: 0

user2437417
user2437417

Reputation:

You get "Nicholas" precisely because JavaScript is never "by reference". If it was, you'd be able to update the person variable from any location. That's not the case, so the new Object() in the function doesn't mutate the outer person variable.

But it's also not the case that variables refer to the objects themselves, but rather variables hold a special type of reference that let you update the object without directly accessing the memory.

That's why although JavaScript is always "by value", you never get a full copy of an object. You're merely getting a copy of that special reference. As a result, you can manipulate the original object passed via the copy of the reference, but you can't actually replace it via the reference.

Upvotes: 2

Related Questions