Skip to content Skip to sidebar Skip to footer

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)

Solution 1:

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

var person = newObject();     

functionsetName(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 = newObject(); // 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.

Solution 2:

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.

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

var person = newObject(); // person pointing to person referencesetName(person); 
alert( person.name); //" Nicholas"

Solution 3:

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:

functionsetName(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 = newObject();
  // the new object is changed
  obj.name = "Greg";
}

Solution 4:

Pass by reference means this:

functiona(obj) {
    obj = 3;
}

var test = newObject();
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.

Post a Comment for "In Javascript, How Is This Argument Passed By Value And Not By Reference?"