[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 aaddress 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.xaddress 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}

 

posted @ 2024-12-09 15:17  Zhentiw  阅读(2)  评论(0编辑  收藏  举报