对于面向对象语言的继承有两种,一种是接口集成,一种是实现继承,ECMAScript没有接口,所以只能实现继承,而实现继承主要是靠原型链来实现的。
1.原型链
基本思想:利用原型让一个引用类型继承另外一个引用类型的属性和方法。
构造函数,原型,实例之间的关系:每个构造函数都有一个原型对象,原型对象包含一个指向构造函数的指针,而实例都包含一个指向原型对象的内部指针。
实现原型链有一种基本模式,其代码大致如下:
function SuperType() { this.property = true; } SuperType.prototype.getSuperValue = function() { return this.property; } function subType() { this.property = false; } //继承了SuperType SubType.prototype = new SuperType(); SubType.prototype.getSubValue = function (){ return this.property; } var instance = new SubType(); alert(instance.getSuperValue());//true
确定原型和实例的关系可以通过instanceof和isPrototypeOf(),只要用instanceof这个操作符来测试实例与原型中出现过的构造函数,结果就会返回true。用isPrototypeOf()方法也会返回true。
alert(instance instanceof Object); //true alert(instance instanceof SuperType); //true alert(instance instanceof SubType); //true
alert(Object.prototype.isPrototypeOf(instance)); //true alert(SuperType.prototype.isPrototypeOf(instance)); //true alert(SubType.prototype.isPrototypeOf(instance)); //true
由于是原型链的关系,我们可以说instance是Object、SuperType、SubType中任何一个类型的实例。
子类型重写超类型的方法时,给原型添加方法的代码一定要放在替换原型的语句之后。
function SuperType(){ this.property = true; } SuperType.prototype.getSuperValue = function(){ return this.property; }; function SubType(){ this.subproperty = false; } //继承SuperType SubType.prototype = new SuperType(); //添加新方法 SubType.prototype.getSubValue = function (){ return this.subproperty; }; //重写超类型的方法 SubType.prototype.getSuperValue = function SuperType(){ this.property = true; }; var instance = new SubType(); alert(instance.getSuperValue()); //false
第二个方法是原型链存在的方法,重写这个方法,SubType调用getSuperValue()方法时,调用的就是重新定义的方法,但是SuperType的实例调用getSuperValue()时,还会用原来那个方法。
var instance1 = new SuperType(); alert(instance1.getSuperValue()); //true
原型链的问题:
- 引用类型值的原型属性会被所有实例共享;
- 在创建子类时,不能向超类型的构造函数中传递参数。
所以实践中很少单独使用原型链。
2 借用构造函数
基本思想:在子类型构造函数的内部调用超类构造函数,通过使用call()和apply()方法可以在新创建的对象上执行构造函数。
function SuperType() { this.colors = ["red","blue","green"]; } function SubType() { SuperType.call(this);//继承了SuperType } var instance1 = new SubType(); instance1.colors.push("black"); alert(instance1.colors);//"red","blue","green","black" var instance2 = new SubType(); alert(instance2.colors);//"red","blue","green"
对于原型链而言,借用构造函数有一个很大优势,就是在子类构造函数中向超类型构造函数传递参数。例子如下:
function SuperType(name){ this.name = name; } function SubType(){ //继承了SuperType,同时还传递参数 SuperType.call(this, "Nicholas"); //实例属性 this.age = 29; } var instance = new SubType(); alert(instance.name); //"Nicholas"; alert(instance.age); //29
借用构造函数的问题:
- 方法都在构造函数中定义,函数复用就无从谈起;
- 在超类原型中定义的方法,子类不可见。
所以构造函数技术也很少用。
3.组合继承
基本思想:将原型链和借用构造函数的技术组合在一块,从而发挥两者之长的一种继承模式。使用原型链实现对原型属性和方法的继承,通过构造函数实现对实例属性的继承。这样通过原型方法实现函数复用,又能保证每个实例都有自己的属性。
function SuperType(name) { this.name = name; this.colors = ["red","blue","green"]; } SuperType.prototype.sayName = function() { alert(this.name); } function SubType(name, age) { SuperType.call(this,name);//继承属性 this.age = age; } //继承方法 SubType.prototype = new SuperType(); Subtype.prototype.constructor = Subtype; Subtype.prototype.sayAge = function() { alert(this.age); } var instance1 = new SubType("Amy",18); instance1.colors.push("black"); alert(instance1.colors);//"red","blue","green","black" instance1.sayName();//"Amy" instance1.sayAge();//18 var instance2 = new SubType("Joy",20); alert(instance2.colors);//"red","blue","green" instance2.sayName();//"Joy" instance2.sayAge();//20
组合继承是JavaScript中最常用的继承模式。
缺点:无论什么情况下,都会调用两次超类型构造函数:一次是在创建子类型原型的时候,另一次是在子类型构造函数内部,子类型最终会包含超类型对象的全部实例属性,但是我们不得不在调用子类型构造函数时重写这些属性。
4.原型式继承
基本想法:借助原型可以基于已有的对象创建新对象,同时还不必须因此创建自定义的类型。为达到这个目的,道格拉斯克罗克福德给出如下函数:
function object(o){ function F(){} F.prototype = o; return new F(); }
在object()函数内部,先创建了一个临时性的构造函数,然后将传入的对象作为这个构造函数的原型,最后返回这个临时类型的一个新实例。如下例子:
var person = { name: "Nicholas", friends: ["Shelby", "Court", "Van"] }; var anotherPerson = object(person); anotherPerson.name = "Greg"; anotherPerson.friends.push("Rob"); var yetAnotherPerson = object(person); yetAnotherPerson.name = "Linda"; yetAnotherPerson.friends.push("Barbie"); alert(person.friends); //"Shelby,Court,Van,Rob,Barbie"
这种模式要求必须有一个对象可以作为另一个对象的基础。如果有这么一个对象的话,可以把它传递给object()函数,然后再根据对象的需求对得到的对象加以修改。
缺点:包含引用类型值的属性始终都会共享,就像使用原型模式一样。
5.寄生式继承
寄生式继承是与原型式继承紧密相关的一种思路,与寄生构造函数和工厂模式类似。
基本思想:创建一个仅用于封装继承过程的函数,该函数在内部以某种方式来增强对象,最后再像真正是它做了所有工作一样返回对象。
function createAnother(original) { var clone = object(original); //通过调用函数创建一个对象 clone.sayHi = function () { //以某种方式来增强这个对象 alert("hi"); }; return clone; //返回这个对象 } var person = { name:"Amy", friends:["Shelby","Court","Van"]; }; var anotherPerson = createAnother(person); anotherPerson.sayHi(); //"hi"
缺点:使用寄生式继承来为对象添加函数,会由于不能做到函数复用而降低效率;这点与构造函数模式类似。
6.寄生组合式继承
基本思想:通过借用构造函数来继承属性,通过原型链的混成形式来继承方法。也就是使用寄生式继承来继承超类型的原型,然后再将结果指定给子类型的原型。
其基本模型如下所示:
function inheritProperty(subType, superType) { var prototype = object(superType.prototype);//创建对象 prototype.constructor = subType;//增强对象 subType.prototype = prototype;//指定对象 }
例子:
function SuperType(name){ this.name = name; this.colors = ["red","blue","green"]; } SuperType.prototype.sayName = function (){ alert(this.name); }; function SubType(name,age){ SuperType.call(this,name); this.age = age; } inheritProperty(SubType,SuperType); SubType.prototype.sayAge = function() { alert(this.age); }此例子只调用了一次SuperType构造函数,并因此避免了在SuperType.prototype上面创建不必要的、多余的属性。与此同时,原型链还能保持不变, 此方法是引用类型最理想的继承方式。