For example, I have this code:
var objectA = {};
var objectB = {};
objectA.pointer = objectB;
objectB.pointer = objectA;
objectA = null;
objectB = null;
I assume that after last two statements, there is no reference to objects originally assigned to objectA and objectB variables. But according to the technical literature, the reference for those objects still exists. Why ? Does objectA.pointer reference or objectB.pointer reference still exists even the objectA and objectB are set to null ?
Yes.
Maybe some ASCII art helps. After performing
the environment contains two variables (
objectA
andobjectA
) that hold references to two objects (denoted asref:XXX
):After adding properties to the objets,
both objects have a
pointer
property each contain a reference to the other object:As you can se see, there is no relation between a
pointer
property and theobjectA
andobjectB
variables.objectA.pointer
doesn't refer to the variableobjectB
, it got a copy of its value (ref:456
), a reference to the object.After setting both variables to
null
,the environment looks like this:
The
pointer
properties still hold the references to the other object. Replacing the values ofobjectA
andobjectB
didn't change that.