i have a javascript code fragment as
var u = {};
var x = y = z = {"cvalue":"cell", "call":function(){alert(this.cvalue);}};
(function(){u=x;/*change all cvalue in x,y, z, u*/ u.cvalue = "notcell";})();
if(u == x && x == y && y == z && z == u){
u.call();
}
//only u goes to null
u = null;
//x,y,z stay same
alert(x.cvalue);
wondering why u = null only applies for u?
Variables don’t actually hold an object, but simply hold a reference to one. By assigning
utonull, you’re dropping the reference thatuhad to the object.A more basic example:
Note how our object isn’t held in
x. It’s held somewhere in memory, andxis referring to it. When we doy = x, we copy the reference toy, and thereforeybegins to refer to the same object. Settingxtonullsimply drops the reference thatxholds to the object, leaving the actual object unaffected. If we were to setytonull, or to anything else, the garbage collector would eventually pick up the object for destruction.