The whole idea of combing JavaScript prototype

Source: Internet
Author: User

It is believed that a lot of people who have a rudimentary understanding of JavaScript prototypes know that prototype,constructor,__proto__ these nouns, and to some extent, can use these objects and properties. It is even known that a method is defined on the prototype of the constructor for use by instance objects. But a lot of people have no clear knowledge of the whole composition of the prototype and the relationship between prototype and constructor, and now let me see what the relationship between them is like. (This article defaults to readers already have a good understanding of the prototype, if you have not yet begun to understand the prototype suggested reading the JavaScript Advanced Programming "chapter on Prototypes")

Okay, now let's analyze the prototype based on a piece of code.

1 function Foo () {}; 2 var New Foo ();
3 Console.log (foo.prototype); 4 Console.log (foo.__proto__); 5 console.log ("Foo.prototype.constructor----" + Foo.prototype.constructor); 6 console.log ("foo.__proto__.constructor----" + foo.__proto__.constructor); 7 console.log ("foo.constructor----" + foo.constructor);

First, the answers to this code are analyzed.

You can see that foo.prototype and foo.__proto__ are the same value, and the value is an object, in fact __proto__ is to conveniently find a property that is specifically set for the prototype of an instantiated object's constructor, so his value must be the same as the prototype of his constructor. How did that prototype come about? Look at the picture

When the Foo function instantiates the Foo object as a constructor, a new object is created and associated with Foo.prototype ( answer 1), and a connection to Foo.prototype is created for Foo ( foo.__proto__( answer 2)). Foo preserves the properties and methods of Foo independently and performs operations independently if the desired attribute is not found inside Foo (the method is only an alias for the function), and if it exists in Foo.prototype, use or modify the properties on the constructor prototype.

Foo.prototype is an object, the answer to the above code can be seen, this object has a non-enumerable property constructor , pointing to the constructor that created it, so Foo.prototype.constructor is the function of Foo ( answer 3). Because foo.__proto__ is equal to Foo.prototype, Foo.__proto__.constructor is the function of Foo ( answer 4). And because Foo can find and use the properties of the Foo.prototype through the prototype chain, ~~foo can use the constructor property, and the constructor attribute points to what has been said before, So foo.constouctor is also the function of Foo ( answer 5).

The whole idea of combing JavaScript prototype

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.