Javascript continuous assignment, javascript assignment

Source: Internet
Author: User

Javascript continuous assignment, javascript assignment

A few days ago, I found such a piece of code when I searched for the interview questions. After the code was executed, I felt totally different from what I thought.

Var a = {n: 1}; var B = a;. x = a = {n: 2}; console. log (. x); console. log (B. x );

Output result:

Undefined

[Object Object]

At first, I thought the statement should assign a {n: 2} To a, and then assign a. x to {n: 2 };

But this is not the case, so I changed the code and added several logs.

Var test; var a = {get test () {console. log ("call a get"); return test;}, set test (value) {console. log ("call a set"); test = value ;}} var test2; var B = {get test2 () {console. log ("call B get"); return test2;}, set test2 (value) {console. log ("call B set"); test2 = value ;}}. test = {n: 1}; B. test2 =. test; console. log ("begin");. test. x =. test = {n: 2 };

In this way, after begin, the execution of this assignment is clear at a glance.

This is the log printed during statement execution.

A get is triggered first, and then a set is triggered.

I guess the execution order of this statement is: first retrieve the left variable, and then execute the value assignment. (before executing this statement, take out the object reference and assign values from right to left)


The above is all the content of this article. I hope you will like it.

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.