学习javascript面向对象 javascript实现继承的方式
本文实例为大家介绍了javascript实现继承的6种方式,分享给大家供大家参考,具体内容如下
1、【原型链继承】实现的本质是重写原型对象,代之以一个新类型的实例。实际上不是SubType的原型的constructor属性被重写了,而是SubType的原型指向了另一个对象——SuperType的原型,而这个原型对象的construtor属性指向的是SuperType
functionSuperType(){
this.property=true;
}
SuperType.prototype.getSuperValue=function(){
returnthis.property;
};
functionSubType(){
this.subproperty=false;
}
//继承了SuperType
SubType.prototype=newSuperType();
SubType.prototype.getSubValue=function(){
returnthis.subproperty;
}
varinstance=newSubType();
alert(instance.getSuperValue());//true
[注意1]谨慎地定义方法,给原型添加方法的代码一定要放在替换原型的语句之后
functionSuperType(){
this.property=true;
}
SuperType.prototype.getSuperValue=function(){
returnthis.property;
};
functionSubType(){
this.subproperty=false;
}
//继承了SuperType
SubType.prototype=newSuperType();
//添加了新方法
SubType.prototype.getSubValue=function(){
returnthis.subproperty;
}
//重写超类型的方法
SubType.prototype.getSuperValue=function(){
returnfalse;
}
varinstance=newSubType();
alert(instance.getSuperValue());//false
[注意2]通过原型链实现继承时,不能使用对象字面量创建原型方法,这样做会重写原型链
functionSuperType(){
this.property=true;
}
SuperType.prototype.getSuperValue=function(){
returnthis.property;
};
functionSubType(){
this.subproperty=false;
}
//继承了SuperType
SubType.prototype=newSuperType();
//使用字面量方法添加新方法会导致上一行代码无效
SubType.prototype={
getSubValue:function(){
returnthis,subproperty;
},
someOtherMethod:function(){
returnfalse;
}
};
varinstance=newSubType();
alert(instance.getSuperValue());//error
[缺点1]在创建子类型的实例时,不能向超类型的构造函数中传递参数
[缺点2]包含引用类型值的原型属性会被所有实例共享
functionSuperType(){
this.colors=['red','blue','green'];
}
functionSubType(){}
//继承了SuperType
SubType.prototype=newSuperType();
varinstance1=newSubType();
instance1.colors.push('black');
alert(instance1.colors);//'red,blue,green,black'
varinstance2=newSubType();
alert(instance2.colors);//'red,blue,green,black'
2、【借用构造函数继承(又叫伪造对象或经典继承)】在子类型构造函数的内部调用超类型构造函数,因此通过使用apply()和call()方法也可以在将来新创建的对象上执行构造函数
functionSuperType(){
this.colors=['red','blue','green'];
}
functionSubType(){
//继承了SuperType
SuperType.call(this);
}
varinstance1=newSubType();
instance1.colors.push('black');
alert(instance1.colors);//'red,blue,green,black'
varinstance2=newSubType();
alert(instance2.colors);//'red,blue,green'
[优点]传递参数
functionSuperType(name){
this.name=name;
}
functionSubType(){
//继承了SUperType,同时还传递了参数
SuperType.call(this,"Nicholas");
//实例属性
this.age=29;
}
varinstance=newSubType();
alert(instance.name);//"Nicholas"
alert(instance.age);//29
[注意]为了确保SuperType构造函数不会重写子类型的属性,可以在调用超类型构造函数后,再添加应该在子类型中定义的属性
functionSuperType(name){
this.name=name;
this.age=30;
}
functionSubType(){
//实例属性
this.age=29;
//继承了SUperType,同时还传递了参数
SuperType.call(this,"Nicholas");
}
varinstance=newSubType();
//实例属性被重写为SuperType构造函数的属性
alert(instance.age);//30
[缺点1]无法实现函数复用
[缺点2]在超类型的原型中定义的方法,对子类型而言也是不可见的,结果所有类型都只能使用构造函数模式
3、【组合继承(又叫伪经典继承)】将原型链和借用构造函数的技术组合到一起,从而发挥二者之长的一种继承模式。其背后的思路是使用原型链实现对原型属性和方法的继承,而通过借用构造函数来实现对实例属性的继承。这样,既通过在原型上定义方法实现了函数复用,又能够保证每个实例都有它自己的属性,成为JavaScript中最常用的继承模式。
functionSuperType(name){
this.name=name;
this.colors=['red','blue','green'];
}
SuperType.prototype.sayName=function(){
alert(this.name);
};
functionSubType(name,age){
//继承属性
SuperType.call(this,name);
this.age=age;
}
//继承方法
SubType.prototype=newSuperType();
SubType.prototype.constructor=SubType;
SubType.prototype.sayAge=function(){
alert(this.age);
}
varinstance1=newSubType("Nicholas",29);
instance1.colors.push("black");
alert(instance1.colors);//'red,blue,green,black'
instance1.sayName();//"Nicholas"
instance1.sayAge();//29
varinstance2=newSubType("Greg",27);
alert(instance2.colors);//'red,blue,green'
instance2.sayName();//"Greg"
instance2.sayAge();//27
[缺点]无论什么情况下,都会调用两次超类型构造函数:一次是在创建子类型原型的时候,另一次是在子类型构造函数内部。子类型最终会包含超类型对象的全部实例属性,但不得不在调用子类型构造函数时重写这些属性。
functionSuperType(name){
this.name=name;
this.colors=["red","blue","green"];
}
SuperType.prototype.sayName=function(){
alert(this.name);
};
functionSubType(name,age){
SuperType.call(this,name);//第二次调用SuperType()
this.age=age;
}
SubType.prototype=newSuperType();//第一次调用SuperType()
SubType.prototype.constructor=SubType;
SubType.prototype.sayAge=function(){
alert(this.age);
};
4、【原型式继承】借助原型可以基于已有的对象创建新对象,同时还不必因此创建自定义类型。从本质上讲,object()对传入其中的对象执行了一次浅复制。
[注意]原型式继承要求必须有一个对象可以作为另一个对象的基础,如果有这么一个对象的话,可以把它传递给object()函数,然后再根据具体需求对得到的对象加以修改即可
functionobject(o){
functionF(){};
F.prototype=o;
returnnewF();
}
varperson={
name:"Nicholas",
friends:["Shelby","Court","Van"]
};
varanotherPerson=object(person);
anotherPerson.name="Greg";
anotherPerson.friends.push("Rob");
varyetAnotherPerson=object(person);
yetAnotherPerson.name="Linda";
yetAnotherPerson.friends.push("Barbie");
alert(person.friends);//"Shelby,Court,Van,Rob,Barbie"
【4.1】【Object.create()方法】:ECMAScript5新增Object.create()方法规范化了原型式继承。这个方法接收两个参数:一个用作新对象原型的对象和(可选的)一个为新对象定义额外属性的对象。在传入一个参数情况下,Object.create()与object()方法的行为相同
functionobject(o){
functionF(){};
F.prototype=o;
returnnewF();
}
varperson={
name:"Nicholas",
friends:["Shelby","Court","Van"]
};
varanotherPerson=Object.create(person);
anotherPerson.name="Greg";
anotherPerson.friends.push("Rob");
varyetAnotherPerson=object(person);
yetAnotherPerson.name="Linda";
yetAnotherPerson.friends.push("Barbie");
alert(person.friends);//"Shelby,Court,Van,Rob,Barbie"
[注意]Object.create()方法的第二个参数与Object.defineProperties()方法的第二个参数格式相同:每个属性都是通过自己的描述符定义的。以这种方式指定的任何属性都会覆盖原型对象上的同名属性。
varperson={
name:"Nicholas",
friends:["Shelby","Court","Van"]
};
varanotherPerson=Object.create(person,{
name:{
value:"Greg"
}
});
alert(anotherPerson.name);//"Greg"
【4.2】低版本浏览器下兼容Object.create()方法
if(typeofObject.create!="function"){
(function(){
varF=function(){};
Object.create=function(o){
if(arguments.length>1){
throwError('Secondargumentnoesupported');
}
if(o===null){
throwError("Cannotsetanull[[Prototype]]");
}
if(typeofo!='Object'){
throwTypeError("Argumentsmustbeanobject");
}
F.prototype=o;
returnnewF();
}
})();
}
5、【寄生式继承】创建一个仅用于封装继承过程的函数,该函数在内部以某种方式来增强对象,最后再像真的是它做了所有工作一样返回对象
[缺点]无法实现函数复用
functionobject(o){
functionF(){};
F.prototype=o;
returnnewF();
}
functioncreateAnother(original){
varclone=object(original);//通过调用函数创建一个新对象
clone.sayHi=function(){//以某种方式来增强这个对象
alert("hi");
};
returnclone;//返回这个对象
}
varperson={
name:"Nicholas",
friends:["Shelby","Court","Van"]
};
varanotherPerson=createAnother(person);
anotherPerson.sayHi();//"hi"
6、【寄生组合式继承】通过借用构造函数来继承属性,通过原型链的混成形式来继承方法。其背后的基本思路是:不必为了指定子类型的原型而调用超类型的构造函数,所需的无非就是超类型原型的一个副本而已。本质上,就是使用寄生式继承来继承超类型的原型,然后再将结果指定给子类型的原型。寄生组合式继承是引用类型最理想的继承范式。
//这个例子中的高效率体现在它只调用了一次Super构造函数,并且因此避免了在SubType.prototype上面创建不必要的、多余的属性。与此同时,原型链还能保持不变。
functionobject(o){
functionF(){};
F.prototype=o;
returnnewF();
}
functioninheritPrototype(subType,superType){
varprototype=object(superType.prototype);//创建对象
prototype.constructor=subType;//增强对象
subType.prototype=prototype;//指定对象
}
functionSuperType(name){
this.name=name;
this.colors=["red","blue","green"];
}
SuperType.prototype.sayName=function(){
alert(this.name);
};
functionSubType(name,age){
SuperType.call(this,name);
this.age=age;
}
inheritPrototype(SubType,SuperType);
SubType.prototype.sayAge=function(){
alert(this.age);
}
以上就是本文的全部内容,javascript实现继承的方式,感谢大家的阅读,小编会再接再厉!