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