下面代码执行后,foo.x的值是?( )
1 var foo = {n: 1};
2 var bar = foo;
3 foo.x = foo = {n: 2};
求foo.x的值
1,2行foo和bar都是指向对象{n:1}
3行foo.x = foo = {n:2}
相当于foo = {n:2}, foo.x = foo;
即是将对象{n:2}的引用赋给foo.x
对象{n:2}被foo.x所引用,但是没有初始化
所以返回foo.x是undefined
下面代码执行后,foo.x的值是?( )
1 var foo = {n: 1};
2 var bar = foo;
3 foo.x = foo = {n: 2};
求foo.x的值
1,2行foo和bar都是指向对象{n:1}
3行foo.x = foo = {n:2}
相当于foo = {n:2}, foo.x = foo;
即是将对象{n:2}的引用赋给foo.x
对象{n:2}被foo.x所引用,但是没有初始化
所以返回foo.x是undefined