We all know that assignment in javascript is from right to left
var a = b = c
The above statement is actually executed like this
b = c
var a = b
But what if there is an object involved? An example I saw yesterday
var foo = { a: 2 }
var bar = foo
foo.x = foo = { b: 3 }
The first two lines are very simple, just point bar to foo.
In the third line, foo = { b: 3 } is executed at the beginning, which redirects foo to another object.
The next sentence foo.x = foo, I originally thought that foo.x points to foo, so that foo becomes an object that cyclically refers to itself, that is,
foo = {
b: 3,
x: {
b: 3,
x: {
b: 3,
x: {
......
}
}
}
}
However, the actual test found that foo.x in the sentence foo. Right, but the actual execution will be from right to left?
I just saw a good explanation from a great master (I am checking the information). The approximate explanation of continuous assignment of objects is as follows: foo.x = foo = { b: 3 }, first during the execution of the interpreter See if foo.x and foo have been created. If not, create it and point them both to the rightmost object. Obviously foo is created and points to { a: 2 }, foo.x has not been created yet, so create it and let it Point to null. At this time, bar and foo both point to {a:2,x:null}, and then point them both to the object {b:3} on the right. At this time, bar points to {a:2,x:{b:3}. },foo points to {b:3};
We just discussed this issue recently, and we came to the conclusion that:
foo.x
中的foo
实际是对{ a: 2 }
is a reference, but the execution order is still from right to left, there is no doubt about this.Assignment is an expression,
Associativity is from right to left, that is,
a = b = c
isa = (b = c)
a = b = c
是a = (b = c)
返回值是,等号右边那个表达式的返回值,即
b = c
的返回值是c
The return value is, etc. The return value of the expression on the right side of the number, that is, the return value ofb = c
is the value ofc
.Local left to right (.), macro right to left (=). This foo is not that foo .
Whenreads the second foo, it has already obtained the reference of
{ a: 2 }
from the first foo and is waiting to assign a value to its x attribute.Assignment is from right to left, but the code must be processed before execution.
foo.x = foo = { b: 3 }
,对属性的处理是按值进行的,即此时已经定位到了{ a: 2 }
The actual storage location of this object. The assignment to attribute x is also the assignment to the x attribute of the actual object, whether it is the object pointed to by foo or bar The object pointed to. Assignment to an object changes the address of the actual object stored in the object, that is, changes the pointer of foo.I don’t think this way of writing makes sense.
In actual production, if you encounter a lot of such code, can you understand it at a glance?
Just like now, how many of the people who answered the previous questions can explain it in layman’s terms?
/a/11...
But actually executing the above three statements in order, the results will be different, because foo.x has been prepared during the parsing stage, so it actually points to bar.x
Actual equivalent code
You can use pointers to understand this problem