The question passed by the parameter of the JS function, how does the reference type pointer change?

function setName(obj) {
  obj.name = "Nicholas";
  obj = new Object(); 
  obj.name = "Greg";
  console.log(obj.name); // "Greg"
}
 
var person = new Object();
setName(person);
alert(person.name) 

setName is a bit difficult to understand: the object person is passed into the setName function as a parameter, and the local parameter obj, is passed by value according to the book, so the value here should be a "pointer". Therefore, obj copies a pointer to person, and the pointer stored in obj also points to person.
then, new instantiates an object, the object pointer is saved in the parameter obj, so this time obj points to a new object, pointing to person is broken?
then, the rest of the train of thought is confused, because obj is only a local parameter, and obj.name outputs Greg.
finally, the alert pops up the Nicholas, description, and the pointer of the previous obj disconnecting the person continues again? How did it continue?
may not be very clear, mainly because of how the pointer changes.

Sep.16,2021

js only values are passed, not by reference, and object is just the value of the reference type (or address value). It means that in the end, it all points to the value (base type value / reference type value), not to the reference of the reference.

function setName(obj) {//3: obj -> person -> obj1 
  obj.name = 'Nicholas';//4: obj -> person -> obj1 && obj1.name = 'Nicholas'
  obj = new Object(); //5: obj -> obj2 
  obj.name = "Greg";//6: obj -> obj2  && obj2.name = 'Nicholas'
  console.log(obj.name); //7: console.log obj.name -> obj2.name 'Greg'
}
 
var person = new Object();//1: person -> obj1 
setName(person);//2: person -> obj1
alert(person.name) //8: alert person -> obj1 -> obj1.name  'Nicholas'

first of all, person passes in the setName function, yes, obj points to person, so it modifies the obj.name equivalent to person.name='Nicholas';, and then obj re-new, breaks, then appends the new value Greg, so the output obj.name is Greg, and obj disconnects the person pointer, but person itself has added the name attribute to Nicholas, in front of it, and it doesn't affect it, so the latter alert is Nicholas


. Parameter passing : in a sentence,
parameter is a reference type (object), then the parameter is passed by value, the value is accessible, and the original object value cannot be modified (or the variable reference object cannot be modified) The obj in the

function is a copy of the pointer person. In the function, it first changes the name of the original object to "Nicholas", and then points to another object, while the person still points to the original object, so what alert comes out is "Nicholas"


clipboard.png


.
var person = new Object();
var obj = person;
obj.name = "name";
obj = new Object();
obj.name = "newName";
console.log(person.name);     // "name"

from the above analysis, it is concluded that obj copies the reference type value of person, and obj assigns a new object, which does not affect person

.
var person = new Object();
function setName(obj) {
    //  var obj = person; obj
    obj.name = "name";
    obj = new Object();
    obj.name = "newName";
}
setName(person);
console.log(person.name);    // "name"

for more details, please refer to

here.
Menu