Class之间可以通过extends关键字,实现继承。
子类会继承父类的属性和方法。
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
class ColorPoint extends Point { constructor(x, y, color) { this.color = color; // ReferenceError super(x, y); this.color = color; // 正确 } }
上面代码中,子类的constructor方法没有调用super之前,就使用this关键字,结果报错,而放在super方法之后就是正确的。
注意:ColorPoint继承了父类Point,但是它的构造函数必须调用super方法。
http://www.tuicool.com/articles/fQ7ZFfM