1.原型链继承
function Parent(){
this.name = 'parent'
}
function Child(){
this.age = 22
}
Child.prototype = new Parent()
console.log(new Child)
2.构造函数继承
function Parent(){
this.name = 'parent'
}
function Child(){
Parent.call(this)
this.age = 22
}
console.log(new Child())
3组合继承
function Parent(){
this.name = 'parent'
}
Parent.prototype.getName = function(){
return this.name
}
function Child(){
Parent.call(this)
this.age = 22
}
Child.prototype = new Parent()
Child.prototype.constructor = Child
console.log(new Child())
4.原型式继承
let parent = {
name: 'parent',
getName: function(){
return this.name
}
}
let child = Object.create(parent)
child.age = 22
console.log(child)
5.寄生式继承
let parent = {
name: 'parent',
age: 22,
getName: function(){
return this.name
}
}
function clone(parent){
let clone = Object.create(parent)
clone.getAge = function(){
return this.age
}
return clone
}
let child = clone(parent)
6.寄生组合继承
function Parent(){
this.name = 'parent'
}
Parent.prototype.getName = function(){
return this.name
}
function Child(){
Parent.call(this)
this.age = 22
}
function clone(parent, child){
child.prototype = Object.create(parent.prototype)
child.prototype.constructor = child
}
clone(Parent, Child)
Child.prototype.getAge = function(){
return this.age
}
console.log(new Child)