子類.prototype = new 父類(); //把父類的全部屬性 都變成子類的公有屬性
複製代碼
B.prototype = Object.create(A.prototype); // 建立一個指定原型的對象 建立一個對象,而且這個對象的 __proto__ 指向 A.prototype
B.prototype.constructor = B; // 原型式繼承一樣是修改 B 類原型的指向,因此須要從新指定構造函數
let b = new B();
複製代碼
class A{
constructor(name,age){
this.name = name;
this.age = age;
}
//公有方法----添加到原型上
say(){
console.log(`${this.name} say`);
}
}
//繼承
class B extends A{
constructor(x,y,forName,forAge){
super(forName,forAge);
this.x = x;
this.y = y;
}
}
let b = new B('x','y','zhangsaan',12);
b.say();
複製代碼
在使用 ES6 的 extends 關鍵字以前,必須使用 super(); super 表示父類的構造函數bash