缺点: 不能传参 没有解决对象的引用问题
借用构造函数继承 //创建一个父类 function Parent(name) { this.name=name ||'jack' this.colors=['red','green','blue'] } Parent.prototype.getName=function() { return this.name; } //创建一个子类 function Child(name) { //借用父类来承继实例属性,但不能继承父类方法 Person.call(this,name) }缺点: 不能继承父类方法
组合继承 = 原型链继承 + 借用构造函数继承 //创建一个父类 function Parent(name) { this.name=name ||'jack' this.colors=['red','green','blue'] } Parent.prototype.getName=function() { return this.name; } var p1=new Parent(); p1.getName(); //创建一个子类 function Child(name) { Parent.call(this,name) } Child.prototype=new Parent(); var c1=new Child() c1.getName()优点:即能继承父类的原型方法,也能传递参数属性