Continuous value assignment and Value Order var A = {n: 1}; A. X = A = {n: 2}; alert (A. X); //-> undefined

Source: Internet
Author: User

Http://liunian.info/%E8%BF%9E%E7%BB%AD%E8%B5%8B%E5%80%BC%E4%B8%8E%E6%B1%82%E5%80%BC%E9%A1%BA%E5%BA%8F.html)

I saw a post in 2010.[1], which discusses a piece of JavaScript code:

1
2
3
VaR
= {N: 1 };
A. X =
= {N: 2 };
Alert (A. X );
// --> Undefined

There are many discussions in the post to analyze why a.x is undefined. Below are some of my thoughts.

First, in Javascript, You need to specify the following:

  1. It can be said that everything is an object, and everything is a reference call.
  2. The value assignment operator (=) has the lowest priority except the comma operator (,) and is right-bound.[2]
  3. Order of Value[3] from left to right[4].

1
2
3
4
5
6
7
8
9
10
11
A. x
= A =
{N: 2 };
// =>
A. X =
(A = {n:
2 });
/*
1. First, group by the combination of =;
2. Then, because of the order of evaluation from left to right and the operator priority, A. X is calculated first to obtain an address pointing to the memory, which is assumed to be;
3. Calculate the right side of the first equal sign, which is the result of evaluating the second equal sign: Point variable A to the object {n: 2}, and then return the reference;
4. then assign the reference to a, that is, the value stored in location a is {n: 2 };
In the process of calculating the second equal sign in the third step, A is directed to another object {n: 2} Instead of {n: 1,
At this time, the position indicated by A. X is no longer a, so the final result of A. X is undefined.
*/

Different from some trick used to streamline the code Display Technology, continuous assignment should be said to be a more common statement, but it should be noted that such modifications and references coexist should be written as little as possible.

 

Http://www.iteye.com/topic/785445? Page = 2 #1712357

==================================== Answer ======================================

Well, this is also a problem that is prone to errors when the concepts of associatipotence and order of evaluation are unclear.
Associativity and order of value are not necessarily related.

The order in which JavaScript expressions are evaluated is from left to right. Although the combination of values is right, values are also calculated from left to right. See Appendix D of ecmascript 5,
11.8.2, 11.8.3, 11.8.5: ecmascript generally uses a left to right evaluation order, however the Edition 3 specification language for the> and <= operators resulted in a partial right to left order. the specification has been corrected for these operators such
That it now specifies a full left to right evaluation order. However, this change of order is potentially observable if side-effects occur during the evaluation process.

The description here shows that ecmascript uses the order of evaluation from left to right.

Ecmascript 5 grammar code
  1. Assignmentexpression:
  2. Conditionalexpression
  3. Lefthandsideexpression assignmentoperator assignmentexpression

This syntax rule defines the combination of the value assignment operator on the right. How can we see it?
First, you must be able to understand the syntax in the ecmascript specification. On the left side of the colon is the name of the syntax rule, and on the right side is the rule deduction content. In the derivation content, the same sub-rule belongs to the same line and different sub-rules belong to different rows; the relationship between different sub-rules is "or.

The preceding syntax rules mean:
A "value assignment expression ",
It can be composed of a "conditional expression;
Alternatively, it can be composed of a "left-hand edge expression" plus a "value assignment operator" and a "value assignment expression.

If the rule itself appears in the derivation content of a syntax rule, the rule is "recursive" directly. If you appear at the leftmost side of a sub-rule in the derived content, it is "left recursion". If you appear on the rightmost side, it is "right recursion ". The combination of operators also exists here: the left recursion rule means the left combination, and the right recursion rule means the right combination.

As you can see, the syntax of the value assignment expression of ecmascript is right recursion, so it is right combination.

What are the effects of the two on program execution? The preceding clue answer is correct. For more information about the relationship between associativity, priorities, and order of value, see my previous post. Virtual Machine discussion (1): Interpreter, tree traversal interpreter, stack-based and register-based, hodgedge, examples are provided in the context of the abstract syntax tree. Like JavaScript, Java and C # Use the order of values from left to right, while the value assignment operation is the combination of the right, so the example of the post can also be used to help understand the status of JavaScript.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.