JS continuous assignment, pointer

Source: Internet
Author: User

There are many successive assignments in the JQ source code, similar to this:

var a = {N:1};  var B = A; Hold A to check back  a.x = a = {N:2};  alert (a.x);//  -undefined alert (b.x);/--[Object Object]  

There are many explanations on the Internet, here I do a record, the complexity of the simple, relatively easy to understand the process is this:

Give {N:1},{n:2} a name for the virtual object that actually holds the two memory addresses, such as: Obj1-"{n:1},obj2-" {N:2}

1) var a = {N:1}; Defines a pointer to an in-memory {N:1}, which is a = Obj1->{n:1}

2) var B = a;//defines b also points to the in-memory {N:1}, which is B = Obj1->{n:1}

3) a.x = {n:2};//Essence is obj1.x = {N:2}, which changes the objects pointed to by A and b

4) A = {n:2};//a pointer pointing to Obj2, is a new object

Now the result is:

A:A = Obj2 = {N:2};

B:b = Obj1 = {

N:1,

X:{n:2}

}

Well, here's the actual process, which is clearly written in the Web (http://snandy.iteye.com/blog/785445?page=2#comments) Comments:

A few simple excerpts:

1. Order of evaluation: from left to right

2, the Union of operators: right combination, but also from left to right evaluation

Main interpretation a.x = a = {N:2}; The order of execution of this sentence

According to the above two rules:

1) evaluation from left to right, that is, the first assignment to a.x that is a.x = expression

2) Read the a.x point at this point to who, at this point A to obj1-"{N:1}, that is, to obj1.x assignment into obj1.x = expression

3) Left Parse, enter "=" to the right, is an expression (A={n:2}), here is why not a bare a, that is why not the order a.x = A, mainly according to the second, right binding,

A = {N:2} is treated as a holistic resolution (the pointer to a is pointed to obj2) and the {N:2} is returned.

4) Assignment, obj1.x = Obj2 = {N:2}, 3rd step in the parsing process has a pointer to the OBJ2, so a.x (obj2.x) does not have this attribute, obj1 only

More difficult to understand is the third step, understand the points: first, not first calculate the right value (that is, not first calculate a = {N:2}), but first parse the left rule (pointer) a.x, parsed into obj1.x, before the beginning to the right,

At this time, according to the right binding, the (A={n:2}) as a whole, and then copy the calculation. Our awkward point is that a "a" in an expression points to a different object, as explained in the commentary. A appears several times, and can be seen as a recursive

JS continuous assignment, pointer

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.