对象通过引用传递。它们永远不会被复制。我有如下代码段:
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
我的问题是在我将对象人的名字更新为“Tom”之后,为什么本地变量 fname 没有更新?