Home > Net >  can anyone explain me why the answer is { name : 'Linda'};
can anyone explain me why the answer is { name : 'Linda'};

Time:09-17

let person = { name : 'Linda'};
let members = person;
person = null

console.log(members)

the output is {name: 'Linda'} but what i've learnt about reference type is that members and person have the same memory address

CodePudding user response:

...but what i've learnt about reference type is that members and person have the same memory address...

Sort of. They both contain a reference to the same object, which is elsewhere in memory (not literally inside the variables). (Whether that's literally a memory address or whatever is an implementation detail you don't need to care about, and may differ from JavaScript engine to JavaScript engine.)

When you do person = null, all you're doing is clearing that reference out of the person variable. Doing that has no effect at all on members or on the object that it refers to.

Let's do this step by step:

let person = { name : 'Linda'};

To handle that, the JavaScript engine creates an object in memory. That object will have some kind of way to refer to it called an object reference. For our purposes, let's say it's Ref1234 (that name has no underlying meaning, I literally just picked it at random so we had a name to use):

                         −−−−−−−−−−−−−−− 
Ref1234−−−−−−−−−−−−−−−−>|   (object)    |
                         −−−−−−−−−−−−−−− 
                        | name: "Linda" |
                         −−−−−−−−−−−−−−− 

Then the JavaScript engine stores that reference in the variable person:

                         −−−−−−−−−−−−−−− 
Ref1234−−−−−−−−−−− −−−−>|   (object)    |
                  |      −−−−−−−−−−−−−−− 
                  |     | name: "Linda" |
                  |      −−−−−−−−−−−−−−− 
person:  Ref1234 − 

Then we do:

let members = person;

Which just copies the reference in person into members as well:

                         −−−−−−−−−−−−−−− 
Ref1234−−−−−−−−−−− − −−>|   (object)    |
                  | |    −−−−−−−−−−−−−−− 
                  | |   | name: "Linda" |
                  | |    −−−−−−−−−−−−−−− 
person:  Ref1234 −  |
                    |
members: Ref1234 −−− 

Then we do:

person = null;

All that does is put null in person:

                         −−−−−−−−−−−−−−− 
Ref1234−−−−−−−−−−−−− −−>|   (object)    |
                    |    −−−−−−−−−−−−−−− 
                    |   | name: "Linda" |
                    |    −−−−−−−−−−−−−−− 
person:  null       |
                    |
members: Ref1234 −−− 

That doesn't change anything else at all. So

console.log(members)

shows the object.

If you then did members = null; or the members variable went out of scope with no closure retaining it, etc., and if it was the only remaining thing containing Ref1234, then the object could be garbage-collected by the JavaScript engine. But that doesn't happen when you do person = null; because something else (members) has a copy of the reference.

CodePudding user response:

When you assign person to members, member will store the address of person at this moment. Then you assign person to null so that person will point to another address but not change the value of existed address. So members still keep the old address and its value

  • Related