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
var objectA = {};
var objectB = {};
the environment contains two variables (objectA
and objectA
) that hold references to two objects (denoted as ref:XXX
):
+--------------+
+-------+---------+ +--->| Object#123 |
|objectA|ref:123 *+--+ +--------------+
+-------+---------+
|objectB|ref:456 *+--+ +--------------+
+-------+---------+ +--->| Object#456 |
+--------------+
After adding properties to the objets,
objectA.pointer = objectB;
objectB.pointer = objectA;
both objects have a pointer
property each contain a reference to the other object:
+-----------------+
| Object#123 |
+--->+-------+---------+<----+
+-------+---------+ | |pointer|ref:456 *+---+ |
|objectA|ref:123 *+-+ +-------+---------+ | |
+-------+---------+ | |
|objectB|ref:456 *+-+ +-----------------+ | |
+-------+---------+ | | Object#456 | | |
+--->+-------+---------+<--+ |
|pointer|ref:123 *+-----+
+-------+---------+
As you can se see, there is no relation between a pointer
property and the objectA
and objectB
variables. objectA.pointer
doesn't refer to the variable objectB
, it got a copy of its value (ref:456
), a reference to the object.
After setting both variables to null
,
objectA = null;
objectB = null;
the environment looks like this:
+-----------------+
| Object#123 |
+-------+---------+<----+
+-------+---------+ |pointer|ref:456 *+---+ |
|objectA| null | +-------+---------+ | |
+-------+---------+ | |
|objectB| null | +-----------------+ | |
+-------+---------+ | Object#456 | | |
+-------+---------+<--+ |
|pointer|ref:123 *+-----+
+-------+---------+
The pointer
properties still hold the references to the other object. Replacing the values of objectA
and objectB
didn't change that.