From a simple example to understand how the JS reference type pointer works

Source: Internet
Author: User

?
1 2 3 4 5 6 7 <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 results are not well understood, it is easy to get people to think about it-"a.x is not pointing 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 understand, if you can see the reason and working mechanism naturally need not continue to look down.

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

First look at the code of A.x=a={n:2} , which is actually equivalent to:

A.x=a;

A={n:2}

Although the arithmetic assignment order in JS should be from left to right, but because of operator "." is the highest level, the leftmost "A.x=a" is executed first, and then "A={n:2}" is executed.

This step shows that we will look at how JS executes this code from beginning to end.

First,

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

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, when both A and B are pointing to object A:

This step is well understood and then continues to look at the next line of code:

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

That's the code we said above that can be split into two lines:

a.x = A; //Of course such writing is not advocated, circular reference, will cause memory leaks

A = {N:2};

This happens at this time--by adding an attribute X to object A by a.x, and this attribute x is pointing to object A, and then because of "a={n:1}", A is no longer pointing to the original object A, but to the new object {N:2} (we call object B):

Here's a fun thing, because object a adds an attribute x, because B is pointing to object A, so we can also use b.x to represent this new property of the A object.

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

While in Console.log (b.x) , because b.x represents the X property of object A, the property is pointing to object a itself. A object has 2 attributes N and x, and naturally it outputs 2 object.

The above is purely an understanding of how the JS reference type works, and if anything is wrong, please indicate thank you:)

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.