# 写了10年Javascript未必全了解的连续赋值运算

source link: https://yanhaijing.com/javascript/2012/04/05/javascript-continuous-assignment-operator/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

05 April 2012

``````var a = {n:1};
a.x = a = {n:2};
alert(a.x); // --> undefined
``````

``````a.x = {n:2};
a = {n:2};
``````

``````a = {n:2};
a.x 未被赋值{n:2}
``````

``````var a = {n:1};
var b = a; // 持有a，以回查
a.x = a = {n:2};
alert(a.x);// --> undefined
alert(b.x);// --> [object Object]
``````

``````a = {n:2};
a.x = {n:2};
``````
``````a.x = (a = {n:2});
``````

``````a.x = a = {n:2};
``````

a.x 中的a指向的是 `{n:1}`，a 指向的是 `{n:2}`

``````              a.x  =  a  = {n:2}
│      │
{n:1}<──┘      └─>{n:2}
``````

``````var a = {n:1};
a.x = a = {n:2};
``````

``````function fun(){
var a = b = 5;
}
fun();
alert(typeof a); // --> undefined
alert(typeof b); // --> number
``````

## About Joyk

Aggregate valuable and interesting links.
Joyk means Joy of geeK