JS的常见继承方式
-
ES6的继承
class Parent { this.age = 18; } class Child extends Parent{ super(); this.name = 'son'; } let child = new Child(); console.log(child.name,child.age);
-
原型链的继承
function Parent(){ this.age = 18; } function Child(){ this.name = 'son'; } let child = new Child(); child.prototype = New Parent(); console.log(child.name,child.age);
-
构造函数的继承
function Parent(){ this.age = 18; } function Child(){ this.name = 'son'; Parent.call(this); } let child = new Child(); console.log(child.name,child.age);
-
组合继承
function Parent(){ this.age = 18; } function Child(){ Parent.call(this); this.name = 'son'; } let child = new Child(); child.prototype = New Parent(); console.log(child.name,child.age);