[Solved] Javascript reference [closed]


y = x;
y = 6;

Here, you’re assigning the value of x to y and then changing y. This doesn’t change x because x is never assigned a new value.

var x = {}, y = {};

Here, you create two new objects and assign their addresses to x and y. The two variables behave like pointers now and don’t hold the object’s values themselves but just its address. So, with x.a = 5 you’re not assigning 5 to x but to the new property a added to the object only referenced by x.

x = y;
y.a = 6;

And, here you actually overwrite that previous object’s reference and make x point to the object referenced by y now. Since, x and y point to the same object now, the changes done through y reflect for x as well.

3

solved Javascript reference [closed]