常见的实现继承的方式有以下几种
- 原型链继承
- 构造函数继承
- 组合式继承
- 原型式继承
- 寄生式继承
- 寄生式组合继承
1. 原型链继承
function Parent(opt) {
this.name = opt?.name || ''
}
Parent.prototype.printName = function () {
return this.name
}
function Son(opt) {
this.age = opt?.age || 1
}
Son.prototype = new Parent()
优点:子类实现对父类属性以及方法的继承。
缺点:子类实例化的时候无法给父类传递参数;父类的所有属性成为子类的原型属性,造成属性共享会相互影响。
2. 构造函数继承
function Parent(opt) {
this.name = opt?.name || ''
}
Parent.prototype.printName = function () {
return this.name
}
function Son(opt) {
this.age = opt?.age || 1
Parent.call(this, opt)
}
优点:子类可以向父类传递参数。
缺点:不能继承父类的原型属性,属于功能缺失。
3. 组合式继承
function Parent(opt) {
this.name = opt?.name || ''
}
Parent.prototype.printName = function () {
return this.name
}
function Son(opt) {
this.age = opt?.age || 1
Parent.call(this, opt)
}
Son.prototype = new Parent()
优点:子类实现对父类属性以及方法的继承。
缺点:每次创建子类实例时,会调用两次父类的构造函数。
4. 原型式继承
function object(o) {
function F() { }
F.prototype = o
return new F()
}
5. 寄生式继承
function clone(origin){
let clone = Object.create(origin)
clone.printName = function(){
return this.name
}
return clone
}
缺点:不能做到函数复用。
6. 寄生式组合继承
function Parent(opt) {
this.name = opt?.name || ''
}
Parent.prototype.printName = function () {
return this.name
}
function Son(opt) {
this.age = opt?.age || 1
Parent.call(this, opt)
}
Son.prototype = Object.create(Parent.prototype)
Son.prototype.constructor = Son
优点:解决了组合式继承的缺点,是目前较优的继承方式