Click here to Skip to main content
15,899,313 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
JavaScript
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) 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
Posted

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900