Understanding how js references the type pointer from a simple example

Source: Internet
Author: User

<Script> var a = {n: 1}; var B = a;. x = a = {n: 2}; console. log (. x); // --> undefined console. log (B. x); // --> [object Object] </script> the above example seems simple, but the result is not easy to understand, so it is easy to think about it. -- ". isn't x pointing to object? Why is log (a. x) undefined ?" "Shouldn't B. x be the same as a. x? There are actually two objects for log generation. "Of course, you can understand them first. If you can see the reason and working mechanism, you don't have to continue looking at it. 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 = a; here, a points to an object {n: 1} (we call it object ), B points to the object pointed to by a, that is, at this time, both a and B point to object A: This step is easy to understand, and then continue to look at the next line of very important code:. x = a = {n: 2}; we know that the assignment order of js is always from right to left, but because ". "is the operator with the highest priority, so this line of code first" calculates ". x. In this case, the property x (although this x is undefined) is added to the object {n: 1} pointed by a: As shown in the figure, because B points to object a like A, the x attribute of A must be represented in addition to. x. You can also use B. 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): continue to execute. x = a. Many people think that "object B also adds an attribute x and points to object B itself", but this is not the case, since js has 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. But when the search reaches the top of the prototype chain-Object. prototype-the specified property B is still not found. prototype. x, it will naturally output undefined; and 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 :)

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.