Tell me about the __proto__ and prototype in JS and the inheritance of the prototype.

Source: Internet
Author: User

Encountered in the interview, ask JS how to achieve inheritance, in fact, the best way is the constructor + prototype, today in the discussion, found that their previous understanding of some of the misunderstandings, specifically written out, recently are relatively busy, etc. hand on the project done, you can do a summary.

First of all, I didn't know the place before.

1 __proto__ This is not a standard implementation, but the major browsers and node have adopted, ECMA6 plan to standardize it, __proto__ corresponding to the standard [[prototype]], which is called the built-in prototype properties, To distinguish it from the prototype of the function, in fact, __proto__ is ultimately pointing to Function.prototype, which is also the first function of the JS function corresponding to JS has a functional programming gene.

The 2 function has both the prototype property and the [[prototype]] property, which is __proto__, where prototype is mainly to implement class inheritance, and __proto__ only objects, of course, JS function is the object, so the function also has __ Proto__, the __proto__ of a function is a pointer to Function.prototype, the object is only __proto__, it is the constructor that points to the object, the object has no prototype property.

You can then look at a few simple examples:

varFather=function(){//parent class that needs to be inherited This. Gate = "OK";}varOne=function(){ This. person= "No";} One.prototype=NewFather ();//Replace the prototype object with the prototype property it's almost you.//implementation of Object.creat () One=NewOne ();//Example of oneConsole.log (one.gate);//output OK to implement inheritanceConsole.log (One.prototype.constructor);//print the replaced Prototype.constructor, which should have been one vartwo=New(One.__proto__constructor)//The error you produce is actually an example of FatherConsole.log (Two.person);//undefined
Console.log (One.prototype);//undefined

3 note the new constructor F, new in three steps 2, first create a new object, the second __proto__ in the new object is replaced by the constructor in the F.prototype.constructor, and then the F of this corresponding to the new object context

4 can see the realization of ECMA5 's object.create, deepen understanding

Talk about the __proto__ and prototype in JS and the inheritance of the 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.