2016-02-29 55 views
0

一些我不下面的代码片段获取: a和b是现在会指向同一个对象。分配顺序

var foo = {n:1}; /// foo points to an object 
var bar = foo; /// bar point to the same object as foo 
foo.x = foo = {n:2}; /// foo is now pointing to a new object 

在上次赋值中,属性x被添加到条上。 为什么?不应该指向n:2?

回答

1
foo.x = foo = {n:2}; /// foo is now pointing to a new object 

可以简化为

foo = {n:2}; // foo point to a NEW object 
foo.x = foo; 

所以x是点到foo。属性链接到它自己的对象。

+0

如果我跑这是在Chrome脚本,看来酒吧拥有新的属性。 FOO:对象 N:2 栏:对象 N:1 X:对象 foo.x:未定义 – badigard