I. Introduction
var a = {n:1}; a.x = a = {n:2}; alert(a.x); // --> undefined
This method is found when you look at jquery source code. The second sentence abovea.x = a = {n:2}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: Left-to-right assignment,a.xAssign a value{n:2}But then a is assigned{n:2}That is, a is overwritten and the value is{n:2}, The new A does not have the X attribute, soundefined.
The procedure is as follows:
a.x = {n: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 is assigned a value.
Conjecture 2: Assign values from right to left. A first assigns values{n:2},a.xAfter a is overwritten{a:1}),a.x = {n:2}Engine restrictionsa.xValue assignment is ignored.
The procedure is as follows:
A = {n: 2}; A. X is not assigned a value {n: 2}
Equivalenta.x = (a = {n:2})That is, the first step is performed, which can be explained as well.a.xIsundefined.
Note that A. X in conjecture 2 is not assigned a value at all.
Iii. Proof
The above two kinds of conjecture are believed to be shared by most people. The discussion in the Group is "Conjecture 1" and I think "conjecture 2 」. Actually, they are all wrong. I ignored the reference relationship.
Add a variable B to point to.
VaR A = {n: 1}; var B = A; // hold a to check. X = A = {n: 2}; alert (. x); // --> undefined alert (B. x); // --> [object]
Founda.xStill yesundefinedWhat's amazing is thatb.xNot assigned (for example:b.x={n:2})But it becomes[object Object]. B is pointinga({n:1})Onlya.x = {n:2}If it is executed, B has the X attribute. Actual execution process: from right to left, A is assigned{n:2}, And thena.xAssigned Value{n:2}.
a = {n:2};a.x = {n:2};
Equivalent
a.x = (a = {n:2});
The difference with conjecture 2 is thata.xIf the value is assigned, no value is assigned in conjecture 2. The most important difference is the first stepa = {n:2}A points to a new object.{n:2}, Step 2a.x = {n:2}A is{a:1}.
That is, the join statement
a.x = a = {n:2};
A In A. X points{n:1}, A points{n:2}.
a.x = a = {n:2} │ │ {n:1}<──┘ └─>{n:2}
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
var a = {n:1}; a.x = a = {n:2};
Think the engine will limita.x(After a is rewritten), this is actually not the case. The objects to be pointed to are different. There are no restrictions on the engine.a.x={n:2}.
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?
function fun(){ var a = b = 5; } fun(); alert(typeof a); // --> undefined alert(typeof b); // --> number
Because the blog space is abroad, it may sometimes cause instability (You know). If you think my blog is helpful to you, click the button below to subscribe to your mailbox, all articles on this site have been output in full text.