I. Introduction
CopyCode Code: var A = {n: 1 };
A. X = A = {n: 2 };
Alert (A. X); // --> undefined
This is the method Cai found when reading jquery source code. The second sentence A. X = A = {n: 2} above is a continuous value assignment expression. What happened to this continuous value assignment expression inside the engine? How is it explained?
Ii. Conjecture
Conjecture 1: assign values from left to right,. X is assigned to {n: 2} first, but then a is assigned to {n: 2}, that is, a is overwritten and the value is {n: 2 }, the new A does not have the X attribute, so it is undefined. The procedure is as follows:
1, A. X = {n: 2 };
2, A = {n: 2 };
The result of this interpretation is consistent with the actual running result, which seems to be correct. Note that A. X in Conjecture 1 has been assigned a value.
Conjecture 2: assign values from right to left. A is assigned {n: 2},. X finds that after a is overwritten (before a is {A: 1}),. X = {n: 2} engine limit. the value of X is ignored. The procedure is as follows:
1, A = {n: 2 };
2, A. X is not assigned a value {n: 2}
Equivalent to A. X = (a = {n: 2}), that is, the first step is performed, which can also be interpreted as undefined. Note that A. X in 2 has never been assigned a value.
Iii. Proof
The above two kinds of conjecture are believed to be common to most people. The discussion in the Group seems to be Conjecture 1, and I think it is conjecture 2. Actually, they are all wrong. I ignored the reference relationship. Add a variable B to point to.Copy codeCode: var A = {n: 1 };
VaR B = A; // save
A. X = A = {n: 2 };
Alert (A. X); // --> undefined
Alert (B. X); // --> [object]
It is found that A. X is still undefined. The magic is that B. X has not been assigned a value (for example, B. x = {n: 2}), but is changed to [object]. B points to a ({n: 1}). If a. x = {n: 2} is executed, B has the X attribute. Actual execution process: from right to left, A is first assigned to {n: 2}, and then a. X is assigned to {n: 2 }.
1, A = {n: 2 };
2, A. X = {n: 2 };
Equivalent
A. X = (a = {n: 2 });
The difference from conjecture 2 is that A. X is assigned a value, and there is no value in conjecture 2. The most important difference is that step a = {n: 2} points to the new object {n: 2}, and step 2. a In x = {n: 2} is {A: 1 }. That is, the join statementCopy codeThe Code is as follows: A. X = A = {n: 2 };
In A. X, A points to {n: 1}, and a points to {n: 2 }. For example
4. confusing
After writing this article, some people may still be dizzy. Because the text description in it is really a detour. When I first understood the assignment statement for this connection
Copy code Code: var A = {n: 1 };
A. X = A = {n: 2 };
The engine restricts the rewriting of A. X (after a is overwritten), but this is not the case. The objects to be pointed to are different. The engine does not limit the rewrite of A. X = {n: 2.
Thank you for choosing Cai, dumb, and Ru. This problem was first raised by Cai. In the gray and gray groups of cainiao, Confucianism is so invested and earnest in every discussion, even if it is a topic raised by others.
5. Conclusion
Ah, end with another continuous assignment question. After fun is executed, variable B Overflows out of fun and becomes a global variable. Did you think of it?Copy codeThe Code is as follows: function fun (){
VaR a = B = 5;
}
Fun ();
Alert (typeof A); // --> undefined
Alert (typeof B); // --> Number