编辑代码

// js继承

// 原型链继承
// 缺点:对于父类的引用类型的变量会被所有子类实例所共享,并且无法给父类传递参数
function Son1() {

}
function Father1(name) {
    this.name = name
}
Son1.prototype = new Father1()
Son1.prototype.constructor = Son1

// 构造函数继承
// 缺点:无法继承父类原型上的属性和方法
function Son2() {
    Father2.apply(this)
}
function Father2() {}

// 组合式继承 = 原型链继承 + 构造函数继承
// 缺点:父类会调用两次,存在重复的属性和方法
function Son3() {
    Father3.apply(this)
}
function Father3() {}
Son3.prototype = new Father3()
Son3.prototype.constructor = Son3

// 寄生组合式继承
function Son4() {
    Father4.apply(this)
}
function Father4() {}
function FN() {}
FN.prototype = Father4.prototype
Son4.prototype = new FN()
Son4.prototype.constructor = Son4

// es6继承
class Father5 {}
class Son5 extends Father5 {}