js继承学习笔记
一、构造函数继承
function Parent() { this.name = 'parent' } Parent.prototype.arr = [1,2,3]; function Child() { Parent.call(this) this.age = 12 } var s = new Child()
缺点:无法继承父类构造函数原型对象上的属性和方法
二、原型链继承
1 function Parent() { 2 this.name = 'parent' 3 } 4 5 Parent.prototype.arr = [1,2,3]; 6 7 function Child() { 8 this.age = 12 9 } 10 Child.prototype = new Parent 11 var s = new Child()
缺点:当多个实例继承Child构造函数后,改变某一个实例的name值,其他实例的name值也会改变
三、组合式继承
1 function Parent() { 2 this.name = 'parent' 3 } 4 5 Parent.prototype.arr = [1,2,3]; 6 7 function Child() { 8 Parent.call(this) 9 this.age = 12 10 } 11 12 Child.prototype = Object.create(Parent.prototype) 13 14 var s = new Child()
推荐使用,缺点...有待发掘