Successive assignments worth pondering--the order of assignment operator operations

Source: Internet
Author: User

Recently, in the past, I have seen the article of previous predecessors, for the continuous assignment ( var a={n:1}; A.x=a={n:2}) This knowledge point, at the beginning is not clear, but eventually convinced themselves, to talk about their own experience. The following code can be answered correctly to ignore this article.

1 var a={n:1}; 2 var b=A; 3 a.x = a = {N:2}; 4 // ? 5 Console.log (b.x); // ?

The correct answer is:

a.x= undefined;b.x= {N:2};

Wonder: Why a.x and b.x are not equal? Why is a.x equal to undefined?

If you have a wrong answer, please look down.

Reference assignment in JavaScript

It is clear that the assignment of an object in JS is actually a reference assignment, but that the reference pointer to the B object points to a reference to the A object.

1 var a={n:1}; 2 var b= A; 3 // 1 4 a.n=2; 5 // 2

The code above is not a problem for a programmer to understand.

Right combination of assignment operators

That is, the assignment operator is right-to-left operation, this is not difficult to understand, such as var a = 1 + 2 , the right side of the 1+2 is calculated first, the right result 3 is assigned to A; You might say this is the operator's priority. In fact, what this article wants to express is that the assignment operation is to assign a value to the left on the right of "=". The following example should help you understand more.

1 var A, B; 2 a=b=1+2;

Which follows the right combination of the assignment operator:

A = b = 1 + 2 equivalent to a = (b= 1+2)

where b equals the return value of 1+2 3;

A equals the return value of b=3 3;

Assignment operator Operation Order: left to right

Although the assignment operator is calculated from right to left, the order of operations is from left to right. In other words, the question raised at the beginning of this article a.x = a = {N:2} is left-to-right (assignment is right-to-left); that is, the order in which the program is evaluated is:

1 a.x 2 a 3 {N:2}

The program executes to a.x = a = {N:2} first executes the a.x and waits for the operation to return the value after the assignment symbol "=", then executes a and waits for the operation to return the value after the assignment symbol "=".

The answer is this.

If you see this as a bit of a detour, analyzing the examples presented at the beginning of this article should help you to better understand:

1 var a={n:1}; 2 var b=A; 3 a.x = a = {N:2};

1. Variable a points to an object {N:1};

2. Variable b points to a reference (in fact {n:1});

3. Calculate a.x and wait for the operation return value after the assignment symbol "=" (at this point, a is pointing to {n:1});

4. Calculate A and wait for the operation return value after the assignment symbol "=", Note that the reference to a is pointed to {N:2};

Do you see here that the 3rd step a.x A is {n:1}; The 4th step of a has been changed to {N:2};

So when Console.log (a.x), it must be undifined, and Console.log (b.x) is {N:2};

If you do not understand, you may add two more variables obj1, obj2; Make Obj1={n:1}; Obj2={n:2}

1 varObj1={n:1};2 varObj2={n:2};3 varA=obj1;4 varb=A;5A.x=a=Obj2;6Console.log (a.x);//undefined7Console.log (b.x);//{N:2}8Console.log (OBJ1);//{n:1, x:{n:2}}9Console.log (OBJ2);//{N:2}

In fact, A.X=A=OBJ2 is equivalent to

A=obj2;

Obj1.x=obj2;

Alvinwei Article source: Weshing's blog http://www.cnblogs.com/alvinwei1024/p/4856623.html

This article is copyright to the author and blog Park, Welcome to reprint

Reprint please explain the original article source

At last

Write code to understand the reference relationship between variables, generally do not write this easy to mislead yourself and others code. I think this kind of situation usually only appears in the interview question, uses in the examination to the basic knowledge the Mastery degree.

Successive assignments worth pondering--the order of assignment operator operations

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.