子类.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