var a = {n: 1} var b = a; a.x = a = {n: 2} console.log(a.x); console.log(b.x)
答案是:
undefined
Object{n:2}
为什么呢?个人见解:
1 object属于引用类型,当然得看下内存怎么分配
2 连等我一直坚持认为从左往右计算,那么a.x = a = {n:2};
==> a.x = (a={n:2});
看内存分配:
可以知道a指向了新内存{n:2}
而b则指向不变,所以b为{n:1,x:{n:2}}