JavaScript reference type pointers how to work _javascript tips

Source: Internet
Author: User
Tags object object

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 well understood, it is easy to think of people around-"a.x not point to object a?" Why is log (a.x) undefined? "," b.x should not be the same as a.x? Why does log come out with 2 objects?

Of course, you can first of all understand, if you can see the reason and the mechanism of work naturally there is no need to continue to look down.

The following is an analysis of the work steps of this simple code to further understand how the JS reference type "assignment" works.

First is

var a = {N:1};
var B = A;

Where a points to an object {N:1} (we call it object a), B points to the object that a points to, that is, at which point A and B are pointing to object A:

This step is well understood, and then keep looking at the next line of very important code:

a.x = a = {N:2};

We know that the assignment order of JS is always from right to left, but because of "." is the highest-priority operator, so this line of code "computes" the a.x first.

This is what happened.--a object {N:1} has new attribute X (although this x is undefined):

As you can see from the diagram, since B is the same as pointing to object A, it is possible to represent the X attribute of a, in addition to a.x, by using b.x.

Then, the A={n:2} is followed by the "Right-to-left" assignment order, at which point a object changed to become the new object {N:2} (what we call object B):

Then continue to execute a.x=a, and many will think this is "Object B also adds a property x, and points to object B itself"

But in fact it is not so, because the first JS has calculated the a.x, it has been resolved that the a.x is object A x, so in the case of the same formula to return to a.x assignment, also does not say that the a.x for object B x.

So a.x=a should understand that object A's attribute x points to object B:

Then the result is obvious. When Console.log (a.x), A is a point to object B, but object B has no attribute x. It doesn't matter, when you look up an object's properties, JavaScript traverses the prototype chain up until it finds the property of the given name. However, when the lookup reaches the top of the prototype chain-that is, object.prototype-still does not find the specified attribute b.prototype.x, it naturally outputs undefined;

In the case of Console.log (b.x), because b.x represents the X property of object A, the property is directed to object B, and naturally it also outputs [object], noting that [object] is not meant to be 2 objects, the string form of the object, is an implicitly called ToString () method of object objects, in the form of: [Object Object]. So [object] represents just an object.

The above mentioned is the entire content of this article, I hope you can enjoy.

Related Article

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.