With Javascript, if I have:
var a = {'x':1, 'y':2};
var b = a;
console.log(a);
console.log(b);
...then both a and b will print the same thing. My understanding is that both a and b are pointing to the same data in memory. However, if I do:
a = {};
console.log(b);
...then b
still has the original object, even though a
is an empty object now. I'd like to be sure that there won't be duplicate copies of the object in memory if I set a variable like above e.g. var b = a
. In some languages, giving a new value to a
would have a knock-on effect to b
, since they both point to the same data in memory.
I'm confused by the fact that setting a = {}
doesn't also set b
to an empty object. Are they actually pointing to the same data until I set a = {}
?