Continuous value assignment operations that are not fully understood by Javascript for 10 years
From: http://www.iteye.com/topic/785445
I like Cai's title very much. Actually, Cai CAI has analyzed it and I borrowed it here. It may mean something about the title party. I will know after reading it.
I. Introduction to Js 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.
Js Code
- Var a = {n: 1 };
- Var B = a; // hold a for future Query
- A. x = a = {n: 2 };
- Alert (a. x); // --> undefined
- Alert (B. x); // --> [object 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 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 statement
Js Code
- 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
Js 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 to all the participants: Cai,, dumb, and Yuru. This problem was first raised by Cai. Every discussion in cainiao's gray and gray groups is so invested and earnest, 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?
Js Code
- Function fun (){
- Var a = B = 5;
- }
- Fun ();
- Alert (typeof a); // --> undefined
- Alert (typeof B); // --> number