foo.x = foo = {n: 2};
determines that foo.x
refers to a property x
of the {n: 1}
object, assigns {n: 2}
to foo
, and assigns the new value of foo
– {n: 2}
– to the property x
of the {n: 1}
object.
The important thing is that the foo
that foo.x
refers to is determined before foo
changes.
See section 11.13.1 of the ES5 spec:
Let lref be the result of evaluating LeftHandSideExpression.
Let rref be the result of evaluating AssignmentExpression.
The assignment operator associates right to left, so you get:
foo.x = (foo = {n: 2})
The left hand side is evaluated before the right hand side.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…