Objects are passed around by reference. They are never copied.
I have a code segment as following:
var person = { firstname: 'John', lastname: 'Smith' }
var anotherPerson = person
anotherPerson.nickname = 'Curly'
console.log(person.nickname)
// "Curly"
var fname=person.firstname
console.log(fname)
// "John"
person.firstname = 'Tom'
console.log(anotherPerson)
// Object { firstname: "Tom", lastname: "Smith", nickname: "Curly" }
console.log(fname)
// "John" <-- fname is not updated
My question is after I updated the object person's firstname
to Tom
, why the local variable fname
is not updated?