The content of this article is about class inheritance analysis in ECMAScript 6 (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Class inheritance
Before looking at class inheritance, first review how the constructor implements object inheritance
function F() { this.a = 1; } function Son() { F.call(this); } function inherit(S, F) { S.prototype = Object.create(F.prototype); S.prototype.constructor = S; } inherit(Son, F); let son = new Son();
What does it implement Function:
Inherit the this attribute of F, which is the attribute of the F instance object
Son.prototype.__proto__ === F.prototype realizes the inheritance of seniority
son.constructor allows son to recognize his ancestors and return to his ancestors
Used with extends and super keywords, look at a simple inheritance
class A { constructor() { this.a = 1; } } class B extends A { constructor() { super(); this.b = 2; } m() { } } let b = new B();
also achieves that Three basic functions
B {a: 1, b: 2} b.__proto__ == B.prototype b.__proto__.__proto__ === A.prototype b.constructor === B
I think: the keyword extends realizes the inheritance of the prototype and the modification of the constructor; the keyword super realizes the inheritance of the parent class this, and the super here is equivalent to A.prototype.constructor. call(this)
If you write a constructor, you must write super in it, otherwise the new subclass instance object will report an error; or do not write it at all; secondly, in the constructor of the subclass The this attribute must be written after super
1. ES5 inheritance, the essence is to first create the instance object this of the subclass, and then add the method of the parent class to this (Parent.apply(this)) . ES6
The inheritance mechanism of Your own this object must first be shaped through the constructor of the parent class to obtain the same instance attributes and methods as the parent class, and then be processed and add the subclass's own instance attributes and methods. If the super method is not called, the subclass will not get this object.
class B extends A { constructor() { //要么都不写,new时默认会自动生成 super(); this.b = 2; //写在super后面 } m() { } }
A.prototype.constructor.call(this)
A.prototype; so only methods on the prototype chain can be called, and the parent class cannot be used. The methods and attributes of the instance
constructor{} cannot be called
class A { constructor() { this.a = 1; } n() { return this; } } class B extends A { constructor() { super(); this.b = 2; } m() { return super.n(); } } let b = new B(); b === b.m();
super is equivalent to this, and the assigned attributes become attributes of the subclass instance
class A { constructor() { this.x = 1; } } class B extends A { constructor() { super(); this.x = 2; super.x = 3; console.log(super.x); // undefined console.log(this.x); // 3 console.log(super.valueOf() instanceof B); //true } } let b = new B();
Only the class can call the static method of the class
class A { constructor() { this.a = 1; } static n() { return this; } } class B extends A { constructor() { super(); this.b = 2; } static m() { return super.n(); } } console.log(A.n() === A) // true console.log(B === B.m()); //true
var obj = { toString() { return "MyObject: " + super.toString(); } }; Object.getPrototypeOf(obj).toString = function () { return "这里super等于obj.__proto__"; } console.log(obj.toString()); //MyObject: 这里super等于obj.__proto__
class A { } class B { } // B 的实例继承 A 的实例 Object.setPrototypeOf(B.prototype, A.prototype); // B 继承 A 的静态属性 Object.setPrototypeOf(B, A); const b = new B();
and es6 uses classes to implement its inheritance,
code Excerpted from es6 Getting Started
class MyArray extends Array { constructor(...args) { super(...args); } } var arr = new MyArray(); arr[0] = 12; arr.length // 1 arr.length = 0; arr[0] // undefined
class NewObj extends Object{ constructor(){ super(...arguments); } } var o = new NewObj({attr: true}); o.attr === true // false
The above is the detailed content of Class inheritance analysis in ECMAScript 6 (with examples). For more information, please follow other related articles on the PHP Chinese website!