[Javascript] Assignment question
var a = { n: 1 };
var b = a;
a.x = a = { n: 2 };
console.log(a.x); // undefined
console.log(b.x); // {n: 2}
Javascript see the following code, mainly will do 3 things
1. Locate a
address reference in stack
2. Create {n: 1}
in heap
3. Assign address reference to a
var a = {n: 1}
var b = a
a.x = a = {n: 2}
First locate a.x
address reference in stack
Second eval a = {n: 2}
Last, assign a.x = <return value of statement>
Finally:
console.log(a.x) // undefined
console.log(b.x) // {n: 2}