为什么reference = null 不影响被引用的对象? [英] Why does reference = null not affect the referenced object?
本文介绍了为什么reference = null 不影响被引用的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道这行得通,但我不知道为什么或这样做的原因:
I know this works, but I don't know why or the reasoning behind why it was made to work this way:
var foo = [5, 10];
var bar = foo;
console.log(foo); //[5, 10]
console.log(bar); //[5, 10]
bar[0] = 1;
console.log(foo); //[1, 10]
bar = null;
console.log(foo); //[1, 10]
我希望不仅 bar
变为 null,而且 foo
也会变为 null.我希望得到一些帮助来理解这一点.
I would have expected not just bar
to become null, but foo
as well. I'd love some help understanding this.
推荐答案
区别在于 重新绑定和变异操作.
bar[0] = 1
正在变异;它会影响 bar
指向的对象.
is mutating; it affects the object that bar
points to.
bar = null
正在重新绑定;它只会影响标识符 bar
的含义.
is rebinding; it just affects what the identifier bar
means.
这篇关于为什么reference = null 不影响被引用的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文