一、原型链
实现方法:让一个原型对象等于另一个类型的实例
function SuperType(){
this.prototype=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;
};
var instance=new SubType();
alert(instance.getSuperValue()); //true
上面的例子中,调用instance.getSuperValue()会经历三个搜索步骤:
1.搜索实例
2.搜索SubType.prototype
3.搜索SuperType.prototype
注意:
在通过原型链实现继承时,不能使用对象字面量创建原型方法,因为这样做会重写原型链。
原型链的问题:
1.包含引用类型值的原型属性会被所有实例共享
2.在创建子类型的实例时,不能向超类型的构造函数中传递参数
二、借用构造函数
这种技术的基本思想是:在子类型构造函数的内部调用超类型构造函数
function SuperType(){
this.colors=["red","blue","green"];
}
function SubType(){
//继承了SuperType
SuperType.call(this);
}
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"
以上代码在(未来将要)新创建的SubType实例的环境下调用了SuperType构造函数。这样一来,就会在新SubType对象上执行SuperType()函数中定义的所有对象初始化代码。结果,SubType的每个实例就会具有自己的colors属性的副本了。
借用构造函数还可以传递参数:
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(this.age); //29
借用构造函数中的问题:
1.如果仅仅借用构造函数,那么将无法避免构造函数模式存在的问题——方法都在构造函数中定义,函数复用无从谈起
2.在超类型的原型中定义的方法,对子类型而言是不可见的,结果所有类型都只能使用构造函数模式。所以这种方法很少单独使用。
三、组合继承
思路是使用原型链实现对原型属性和方法的继承,而通过借用构造函数来实现对实例属性的继承
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("Nicholas",29);
instance1.colors.push("black");
alert(instance1.colors); //"red,blue,green,black"
instance1.sayName(); //"Nicholas"
instance1.sayAge(); //29
var instance2=new SubType("Greg",27);
alert(instance2.colors); //"red,blue,green"
instance2.sayName(); //"Greg"
instance2.sayAge(); //27
组合继承避免了原型链和借用构造函数的缺陷,融合了它们的优点,成为javascript中最常用的继承模式。而且,instanceof和isPrototypeof()也能够用于识别基于组合继承创建的对象。
四、原型式继承
借助原型可以基于已有的对象创建新对象,同时还不必因此创建自定义类型:
function object(o){
function F(){}
F.prototype=o;
return new F();
}
ECMAScript 5 通过新增Object.create()方法规范化了原型式继承。这个方法接受两个参数:一个用作新对象原型的对象和(可选的)一个为新对象定义额外属性的对象。
var person={
name:"Nicholas",
friends:["Shelby","Court","Van"]
};
var anotherPerson=Object.create(person);
anotherPerson.name="Greg";
anotherPerson.friends.push("Rob");
var yetAnotherPerson=Object.create(person);
yetAnotherPerson.name="Linda";
yetAnotherPerson.friends.push("Barbie");
alert(person.friends); //"Shelby,Court,Van,Rob,Barbie"
Object.create()方法的第二个参数与Object.defineProperties()方法的第二个参数格式相同:每个属性都是通过自己的描述符定义的。以这种方式指定的任何属性都会覆盖原型对象上的同名属性。例如:
var person={
name:"Nicholas",
friends:["Shelby","Court","Van"]
};
var anotherPerson=Object.create(person,{
name:{
value:"Greg"
}
});
alert(anotherPerson.name); //"Greg"
缺点:
包含引用类型值的属性始终都会共享相应的值,就像使用原型模式一样。
五、寄生式继承
寄生式继承的思路与寄生构造函数和工厂模式类似,即创建一个仅用于封装继承过程的函数,该函数在内部以某种方式来增强对象,最后再像真的是它做了所有工作一样返回对象。以下代码示范了寄生式继承模式。
function createAnother(original){
var clone=object(original); //通过调用函数创建一个新函数
clone.sayHi=function(){ //以某种方式来增强这个对象
alert("hi");
}
return clone;
}
可以像下面这样来使用createAnother()函数:
var person={
name:"Nicholas",
friends:["Shelby","Court","Van"]
};
var antherPerson=createAnother(person);
antherPerson.sayHi; //"hi"
优点:
在主要考虑对象而不是自定义类型和构造函数的情况下,寄生式继承也是一种有用的模式。前面示范继承模式时使用的object()函数不是必须的;任何能够返回新对象的函数都适用于此模式。
缺点:
使用寄生式继承来为对象添加函数,由于不能做到函数复用而降低效率;这一点与构造函数模式类似。
六、寄生组合式继承
寄生组合式继承的基本模式如下:
function inheritPrototype(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;
}
inheritPrototype(SubType,SuperType);
SubType.prototype.sayAge=function(){
alert(this.age);
};
开发人员普遍认为寄生式组合继承是引用类型最理想的继承范式。