原型链继承
优点: 将父亲的实例作为孩子的原型 缺点: 父类所有属性都会被子类共享,更改一个子类的属性,其他属性也变了
function father () {
this.name = 'xxx'
this.obj = {
name:
}
}
function Son () {}
Son.prototype = new father()
var son = new Son()
son.name = 'yyy'
借用构造函数
优点: 直接使用父类的实例属性,可以传参 缺点: 不能复用方法、父类上的方法不能使用
function Father (name) {
this.name = 'fuyu'
}
function son (name) {
Father.call(this, name)
}
组合继承
优化:保留了可以传参、保留了原型链的方法,不共享父类的引用属性 缺点:调用了父类两次,产生了2次实例
function Father() {
this.name = 'xxx'
this.arr = [1]
}
function Son (name) {
Father.call(this, name)
}
Son.prototype = new Father()
Son.prototype.constructor = Son
优化1
function Father() {
this.name = 'xxx'
}
function Son () {
Father.call(this)
}
Son.prototype = Father.prototype
Son.prototype.constrcutor = Son
完美模式
function Father () {
this.name = 'xxx'
}
function Son () {
Father.call(this)
}
Son.prototype = Object.create(Father.protptype)
Son.prototype.constrcutor = Son