_javascript techniques for JavaScript continuous assignment problems

Source: Internet
Author: User
Tags object object

A few days ago in search of the face of the test to find such a piece of code, after the implementation of the feeling is completely not the same as the

 var a = {
   n:1
 };
 var b = A;
 a.x = a = {N:2};
 Console.log (a.x);
 Console.log (b.x);

The output results are:

Undefined

[Object Object]

At first, the statement should be assigned a value of {N:2} first, and then the a.x assignment {N:2};

But the fact is not like that, so changed the code, add a few log

var test;
var a = {get
  Test () {
    console.log ("called a Get");
    return test;
  },
  set test (value) {
    Console.log (' Call a set ');
    Test = value;
  }
}
var test2;
var B = {get
  test2 () {
    console.log ("called B Get");
    return test2;
  },
  set Test2 (value) {
    Console.log (' call B Set ');
    Test2 = value;
  }
}
A.test = {
  n:1
};
B.test2 = a.test;
Console.log ("Begin");
A.test.x = A.test = {N:2};

Thus, at the end of the begin, the value of the assignment is at a glance.

This is the log that is printed when the statement is executed

A get is triggered first, and set is triggered once.

I guess the order in which the statement was executed was to take the left variable out first and then perform the assignment. (Take the object reference out before executing the statement, and then execute the assignment from right to left)


The above mentioned is the entire content of this article, I hope you can enjoy

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.