How javascript references the type pointer, and how javascript works

Source: Internet
Author: User

How javascript references the type pointer, and how javascript works

Let's take a look at an example:

 <script> var a = {n:1};  var b = a;  a.x = a = {n:2};  console.log(a.x);// --> undefined  console.log(b.x);// --> [object Object]  </script>

The above example seems simple, but the result is not easy to understand, and it is easy to confuse people-"isn't a. x pointing to object? Why is log (a. x) undefined ?" "Shouldn't B. x be the same as a. x? Why are there two objects in log"

Of course, you can understand it on your own first. If you can see the reasons and working mechanisms, you don't have to continue looking at them.

Next we will analyze the work steps of this simple code to further understand the working method of the js reference type "value assignment.

First

Var a = {n: 1 };
Var B =;

Here, a points to an object {n: 1} (we call it object A), and B points to the object pointed to by a, that is, at this time, both a and B point to object:

This step is easy to understand, and I will continue to look at the next line of very important code:

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

We know that the assignment Operation Sequence of js is always from right to left, but because "." is the highest priority operator, this line of code first "calculates" a. x.

In this case, the property x (although this x is undefined) is added to the object {n: 1} pointed by ):

As you can see from the figure, because B points to object a like A, the x attribute of A can also be represented by B. x in addition to a. x.

Next, execute a = {n: 2} First according to the assignment order of "from right to left". At this time, the object pointed to by a has changed to a new object {n: 2} (object B ):

Then, execute a. x = a. Many people will think that "object B also adds an attribute x and points to object B itself"

But this is not actually the case because js has already computed. x,. x is the x of object A, so it is returned to a in the same formula. the value of x does not mean that. x is the x of object B.

Therefore, a. x = a should be understood as the property x of object A pointing to object B:

Then the result is obvious. When console. log (a. x), a points to object B, but object B has no attribute x. It doesn't matter. When you look for an object's properties, JavaScript will traverse the prototype chain up until the property of the given name is found. However, when the search reaches the top of the prototype chain-that is, Object. prototype-the specified attribute B. prototype. x is still not found, undefined is naturally output;

In the console. log (B. x), because B. x indicates the x attribute of object A. This attribute is directed to Object B. Naturally, [object Object] is output. Note that [object Object] here is not the meaning of two objects, the string form of the Object is the implicit call of the toString () method of the object, the form is: "[Object]". Therefore, [object] represents only one Object.

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.