in JavaScript, the concepts of prototype, constructor, __proto__, constructors, prototypes, and instances have been a headache, plus ES6 class and extends have been messed up porridge, At ordinary times the use of less writing less, is to be well smoothed out clearly. Read several articles with their own understanding, understand as follows: constructor. prototype = prototype;constructor. Prototype.constructor = prototype. constructor = constructor;new constructor = instance;instance. constructor = constructor;example. __proto__ = prototype; this looks not intuitive, then directly on the map, more intuitive to clear the relationship between these concepts: The following code validates the above relationship:
functionCREATEGF (name, age) { This. Name =name; This. Age =Age ;} CreateGf.prototype.sayWhat=function(name) {Console.log ( This. Name + ' say:hi! ');} varGF1 =NewCREATEGF (' GF1 ', 20);varGF2 =NewCREATEGF (' Gf2 ', 22); Gf1.saywhat (gf1.name); Gf2.saywhat (Gf2.name); Console.log (CreateGf.prototype.constructor= = CREATEGF);//trueConsole.log (Gf1.constructor = = CREATEGF);//trueConsole.log (gf1.__proto__ = = Creategf.prototype);//true
What is it like in the ES6? See: roughly the same as in ES5, with code validation:
class Point {constructor (x, y) { This. x =x; This. y =y; } toString () {return' (' + This. x + ', ' + This. Y + ') '; }} class ColorPoint extends point{constructor (x, y, color) {super (Y); This. color =color; } toString () {return This. Color + ': ' +super.tostring (); }} let ColorPoint=NewColorPoint (); Console.log (colorpoint.prototype.__proto__= = Point.prototype);//trueConsole.log (colorpoint.__proto__ = = point);//true
Inheritance in ES5 and inheritance in ES6