指出对象为null,但第二个对象显示属性值 [英] pointed out object is null but the second object showing the property values

查看:78
本文介绍了指出对象为null,但第二个对象显示属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var obj={name: "faizan", age:31}
    var obj2= obj;//obj2 pointing to the same memoray location of obj
    console.log("before making null obj",obj2.name);
    obj={};  //obj became null
      console.log("after making null obj",obj2.name);//now this will need to be null but is working why??



i made object(obj)然后将它分配给第二个对象(obj2) 并最终使obj为null但是在obj2.name之后显示faizan。为什么?它需要显示任何原因obj null现在


i made object (obj) then assigned it to the second object (obj2) and finally make obj null but after that obj2.name showing me "faizan". why? it need to show nothing cause obj null now

推荐答案

因为你将对象与对象的引用混淆。



如果你有一张纸,并且你写下了存放DVD的橱柜的名称,那么你有一个指针到橱柜 - 正如你在第一行代码中所做的那样。

您可以通过将名称复制到另一张纸上来复制指针,然后您有两个指针 - 就像您在第二行代码中所做的那样。

但是,如果你在一张纸上擦掉所有的文字 - 就像你将 obj 设置为不同的值那样 - 这不会影响另一张一张纸,或橱柜本身。
Because you are confusing the object with the reference to the object.

If you have piece of paper, and you write down the name of a cupboard in which you store your DVDs, then you have a "pointer" to the cupboard - just as you do in the first line of code.
You can duplicate the pointer by copying the name onto a separate sheet of paper, and then you have two "pointers" - just as you do in teh second line of code.
But if you then rub out all the writing on one sheet of paper - as you do when you set the obj to a different value - that doesn't affect the other piece of paper, or the cupboard itself.


这篇关于指出对象为null,但第二个对象显示属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆