说我做一个对象,将其复制(如附图),然后废掉它:为什么重新分配时不会将此对象复制为空?
let obj = {
prop: 'one'
}
let otherObj = obj
console.log(otherObj === obj); //true
console.log(obj.prop); //one
obj = null;
console.log(otherObj.prop); //shouldn't this be Uncaught TypeError: Cannot read property 'prop' of null"?
不宜otherObj
也为空,因为它们是同一个对象?
不,你只是改变var指向的内容,而不是目标本身。如果你修改了这个对象,两个变量都会反映出来。把第二个var作为别名;你删除了昵称,而不是该人。 – dandavis