Class 能够经过 extends
关键字实现继承。javascript
class Point {
}
class ColorPoint extends Point {
}
子类必须在constructor
方法中调用super
方法,不然新建实例时会报错。java
class Point { /* ... */ } class ColorPoint extends Point { constructor() { } } let cp = new ColorPoint(); // ReferenceError
this
,而后再将父类的方法添加到this
上面(Parent.apply(this)
)。this
上面(因此必须先调用super
方法),而后再用子类的构造函数修改this
。若是子类没有定义constructor
方法,这个方法会被默认添加app
class ColorPoint extends Point { } // 等同于 class ColorPoint extends Point { constructor(...args) { super(...args); } }
在子类的构造函数中,只有调用super
以后,才可使用this
关键字,不然会报错。函数
父类的静态方法,也会被子类继承。this
class A { static hello() { console.log('hello world'); } } class B extends A { } B.hello() // hello world
用来从子类上获取父类。spa
Object.getPrototypeOf(ColorPoint) === Point // true
super
这个关键字,既能够看成函数使用,也能够看成对象使用。prototype
class A {}
class B extends A {
constructor() {
super();
}
}
super()
只能用在子类的构造函数之中,用在其余地方就会报错。code
class A {} class B extends A { m() { super(); // 报错 } }
class A { p() { return 2; } } class B extends A { constructor() { super(); console.log(super.p()); // 2 } } let b = new B();
因为super
指向父类的原型对象,因此定义在父类实例上的方法或属性,是没法经过super
调用的。对象
class A { constructor() { this.p = 2; } } class B extends A { get m() { return super.p; } } let b = new B(); b.m // undefined
若是属性定义在父类的原型对象上,super
就能够取到。blog
class A {} A.prototype.x = 2; class B extends A { constructor() { super(); console.log(super.x) // 2 } } let b = new B();
在子类普通方法中经过super
调用父类的方法时,方法内部的this
指向当前的子类实例。
class A { constructor() { this.x = 1; } print() { console.log(this.x); } } class B extends A { constructor() { super(); this.x = 2; } m() { super.print(); } } let b = new B(); b.m() // 2
因为this
指向子类实例,因此若是经过super
对某个属性赋值,这时super
就是this
,赋值的属性会变成子类实例的属性。
若是super
做为对象,用在静态方法之中,这时super
将指向父类,而不是父类的原型对象。
在子类的静态方法中经过super
调用父类的方法时,方法内部的this
指向当前的子类,而不是子类的实例。
注意,使用super
的时候,必须显式指定是做为函数、仍是做为对象使用,不然会报错。
因为对象老是继承其余对象的,因此能够在任意一个对象中,使用super
关键字。
__proto__
属性,指向对应的构造函数的prototype
属性。
Class 做为构造函数的语法糖,同时有prototype
属性和__proto__
属性,所以同时存在两条继承链。
__proto__
属性,表示构造函数的继承,老是指向父类。prototype
属性的__proto__
属性,表示方法的继承,老是指向父类的prototype
属性。class A { } class B extends A { } B.__proto__ === A // true B.prototype.__proto__ === A.prototype // true
类的继承是按照下面的模式实现的:
class A { } class B { } // B 的实例继承 A 的实例 Object.setPrototypeOf(B.prototype, A.prototype); // B 继承 A 的静态属性 Object.setPrototypeOf(B, A); const b = new B();
B
)的原型(__proto__
属性)是父类(A
);B
)的原型对象(prototype
属性)是父类的原型对象(prototype
属性)的实例。extends
关键字后面能够跟多种类型的值。只要是一个有prototype
属性的函数,就能被继承。
Object
类:class A extends Object { } A.__proto__ === Object // true A.prototype.__proto__ === Object.prototype // true
class A { } A.__proto__ === Function.prototype // true A.prototype.__proto__ === Object.prototype // true
子类实例的__proto__
属性的__proto__
属性,指向父类实例的__proto__
属性。
//ColorPoint 继承
var p1 = new Point(2, 3); var p2 = new ColorPoint(2, 3, 'red'); p2.__proto__ === p1.__proto__ // false p2.__proto__.__proto__ === p1.__proto__ // truePoint
所以,经过子类实例的__proto__.__proto__
属性,能够修改父类实例的行为。
p2.__proto__.__proto__.printName = function () { console.log('Ha'); }; p1.printName() // "Ha"
ECMAScript 的原生构造函数大体有下面这些:
ES5中,生构造函数是没法继承的。
ES6中,容许继承原生构造函数定义子类。
class MyArray extends Array { constructor(...args) { super(...args); } } var arr = new MyArray(); arr[0] = 12; arr.length // 1 arr.length = 0; arr[0] // undefined
extends
关键字不只能够用来继承类,还能够用来继承原生的构造函数。
注意,继承Object
的子类,有一个行为差别。没法经过super
方法向父类Object
传参
Mixin 指的是多个对象合成一个新的对象,新对象具备各个组成成员的接口。
const a = { a: 'a' }; const b = { b: 'b' }; const c = {...a, ...b}; // {a: 'a', b: 'b'}
function mix(...mixins) { class Mix {} for (let mixin of mixins) { copyProperties(Mix.prototype, mixin); // 拷贝实例属性 copyProperties(Mix.prototype, Reflect.getPrototypeOf(mixin)); // 拷贝原型属性 } return Mix; } function copyProperties(target, source) { for (let key of Reflect.ownKeys(source)) { if ( key !== "constructor" && key !== "prototype" && key !== "name" ) { let desc = Object.getOwnPropertyDescriptor(source, key); Object.defineProperty(target, key, desc); } } }
// 使用
class DistributedEdit extends mix(Loggable, Serializable) { // ... }