After writing Javascript for 10 years, continuous value assignment operations that are not fully understood-> Demonstrate that the variables in JS are references.

Source: Internet
Author: User

Continuous value assignment operations that are not fully understood by Javascript for 10 years

From: http://www.iteye.com/topic/785445

I like Cai's title very much. Actually, Cai CAI has analyzed it and I borrowed it here. It may mean something about the title party. I will know after reading it.

 

I. Introduction to Js Code
  1. Var a = {n: 1 };
  2. A. x = a = {n: 2 };
  3. Alert (a. x); // --> undefined

 

This is the method Cai found when reading jQuery source code. The second sentence a. x = a = {n: 2} above is a continuous value assignment expression. What happened to this continuous value assignment expression inside the engine? How is it explained?

 

Ii. Conjecture

Conjecture 1: assign values from left to right,. x is assigned to {n: 2} first, but then a is assigned to {n: 2}, that is, a is overwritten and the value is {n: 2 }, the new a does not have the x attribute, so it is undefined. The procedure is as follows:

 

1, a. x = {n: 2 };
2, a = {n: 2 };

 

The result of this interpretation is consistent with the actual running result, which seems to be correct. Note that a. x in Conjecture 1 has been assigned a value.

 

Conjecture 2: assign values from right to left. a is assigned {n: 2},. x finds that after a is overwritten (before a is {a: 1}),. x = {n: 2} engine limit. the value of x is ignored. The procedure is as follows:

 

1, a = {n: 2 };
2, a. x is not assigned a value {n: 2}

 

Equivalent to a. x = (a = {n: 2}), that is, the first step is performed, which can also be interpreted as undefined. Note that a. x in 2 has never been assigned a value.

 

Iii. Proof

The above two kinds of conjecture are believed to be common to most people. The discussion in the Group seems to be Conjecture 1, and I think it is conjecture 2. Actually, they are all wrong. I ignored the reference relationship. Add a variable B to point to.

Js Code
  1. Var a = {n: 1 };
  2. Var B = a; // hold a for future Query
  3. A. x = a = {n: 2 };
  4. Alert (a. x); // --> undefined
  5. Alert (B. x); // --> [object Object]

 

It is found that a. x is still undefined. The magic is that B. x has not been assigned a value (for example, B. x = {n: 2}), but is changed to [object Object]. B points to a ({n: 1}). If a. x = {n: 2} is executed, B has the x attribute. Actual execution process: from right to left, a is first assigned to {n: 2}, and then a. x is assigned to {n: 2 }.

 

1, a = {n: 2 };
2, a. x = {n: 2 };

Equivalent


A. x = (a = {n: 2 });

 

The difference from conjecture 2 is that a. x is assigned a value, and there is no value in conjecture 2. The most important difference is that step a = {n: 2} points to the new object {n: 2}, and step 2. a In x = {n: 2} is {a: 1 }. That is, the join statement

Js Code
  1. A. x = a = {n: 2 };

 

In a. x, a points to {n: 1}, and a points to {n: 2 }. For example

 

 

4. confusing

After writing this article, some people may still be dizzy. Because the text description in it is really a detour. When I first understood the assignment statement for this connection

Js Code
  1. Var a = {n: 1 };
  2. A. x = a = {n: 2 };

 

The engine restricts the rewriting of a. x (after a is overwritten), but this is not the case. The objects to be pointed to are different. The engine does not limit the rewrite of a. x = {n: 2.
Thank you to all the participants: Cai,, dumb, and Yuru. This problem was first raised by Cai. Every discussion in cainiao's gray and gray groups is so invested and earnest, even if it is a topic raised by others.

 

5. Conclusion

Ah, end with another continuous assignment question. After fun is executed, variable B Overflows out of fun and becomes a global variable. Did you think of it?

Js Code
  1. Function fun (){
  2. Var a = B = 5;
  3. }
  4. Fun ();
  5. Alert (typeof a); // --> undefined
  6. Alert (typeof B); // --> number

 

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.