I\'ve finally been curious enough to find out why javascript does its voodoo magic to learn why not all object references are created equal.
Given the example:
The value of a
that is assigned to b
is a Number. The value assigned from c
to d
is a reference to an Object.
var a, b, c, d;
a = 100; // a has value 100, a number
b = a; // b has value 100, a number
c = {}; // c has value p, a reference to some object P
d = c; // d has value p, a reference to P
b = 10; // b has value 10, a number
d.e = 'f'; // P.e has value 'f', a string
All variables in JavaScript are not objects. There are native types as well.
c
and d
are not linked to one another. They are pointing to the same object reference. If you were to reassign d
to something else, it will not affect c
.
var c = {};
var d = c;
d = { foo: "bar" };
c === d // false
However, if you were to modify the object being referenced by c
or d
, it will modify the same object since c
and d
are both referring to the same object as in your example.
It looks to me that the difference is with b
, you're reassigning the variable to a new object/value, while with d
, you're modifying the existing object.