<!-- 原型链继承 -->
<script>
function Parent() {
this.name = ['kevin','jane']
}
Parent.prototype.getName = function () {
console.log(this.name)
}
function Child() {}
Child.prototype = new Parent()
Child.prototype.constructor = Child
var child1 = new Child()
console.log(child1.getName())
</script>
<!-- 借用构造函数继承 -->
<script>
function Parent(name){
this.name = name
}
Parent.prototype.getName = function(){}
function Child(name,age){
this.age = age
Parent.call(this,name)
}
var child = new Child('java',18)
</script>
<!-- 组合继承 常用继承方法 -->
<script>
function Parent(name){
this.name = name
this.colors = ['red','green']
}
Parent.prototype.getName = function(){console.log(this.name)}
function Child(name,age){
this.age = age
Parent.call(this,name)
}
Child.prototype = new Parent()
Child.prototype.constructor = Child
var child1 = new Child('java',18)
</script>
<!-- 寄生组合继承 -->
<script>
function Parent(name){
this.name = name
this.colors = ['red','green']
}
Parent.prototype.getName = function(){console.log(this.name)}
function Child(name,age){
this.age = age
Parent.call(this,name)
}
var Fn = function(){}
Fn.prototype = Parent.prototype
Child.prototype = new Fn()
Child.prototype.constructor = Child
var child1 = new Child('java',18)
</script>
来自JavaScript深入之继承的多种方式以及优缺点