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.