原型链继承
function Parent() {
this.name = 'Harry'
this.arr = [1, 2, 3]
}
Parent.prototype.sayName = function () {
console.log(this.name)
}
function Child() {}
Child.prototype = new Parent()
借用构造函数继承
function Parent(name) {
this.name = name
this.arr = [1, 2, 3]
}
Parent.prototype.sayName = function() {
console.log(this.name)
}
function Child(name) {
Parent.call(this, name)
}
组合继承
function Parent(name) {
this.name = name
}
Parent.prototype.sayName = function() {
console.log(this.name)
}
function Child(name, age) {
Parent.call(this, name)
this.age = age
}
Child.prototype = new Parent()
Child.prototype.constructor = Child
原型式继承
function createObj(o) {
function F() {}
F.prototype = o
return new F()
}
寄生式继承
function createObj(o) {
var clone = Object.create(o)
clone.sayHi = function() {
console.log('Hi')
}
return clone
}
寄生组合式继承
function inheritPrototype (subType, superType) {
var prototype = Object.create(superType.prototype)
prototype.constructor = subType
subType.prototype = prototype
}