javascript的几种继承方法介绍
1.原型链继承:构造函数、原型和实例的关系:每个构造函数都有一个原型对象,原型对象都包含一个指向构造函数的指针,而实例都包含一个指向原型对象的内部指针。确认原型和实例之间的关系用instanceof。
原型链继承缺点:字面量重写原型会中断关系,使用引用类型的原型,并且子类型还无法给超类型传递参数
functionParent(){
this.name='mike';
}
functionChild(){
this.age=12;
}
//儿子继承父亲(原型链)
Child.prototype=newParent();//Child继承Parent,通过原型形成链条
vartest=newChild();
console.log(test.age);
console.log(test.name);//得到被继承的属性
//孙子继续原型链继承儿子
functionBrother(){
this.weight=60;
}
Brother.prototype=newChild();//继承原型链继承
varbrother=newBrother();
console.log(brother.name);//继承了Parent和Child,弹出mike
console.log(brother.age);//12
console.log(brotherinstanceofChild);//ture
console.log(brotherinstanceofParent);//ture
console.log(brotherinstanceofObject);//ture
2.构造函数实现继承:又叫伪造对象或经典继承。
构造函数实现继承缺点:借用构造函数虽然解决了原型链继承的两种问题,但没有原型,则复用无从谈起,所以需要原型链+借用构造函数模式。
functionParent(age){
this.name=['mike','jack','smith'];
this.age=age;
}
functionChild(age){
Parent.call(this,age);//把this指向Parent,同时还可以传递参数
}
vartest=newChild(21);
console.log(test.age);//21
console.log(test.name);
test.name.push('bill');
console.log(test.name);//mike,jack,smith,bill
3.组合继承:使用原型链实现对原型属性和方法的继承,而通过借用构造函数来实现对实例属性的继承。这样即通过在原型上定义方法实现了函数复用,又保证每个实现都有它自己的属性。
缺点:无论什么情况下,都会调用两次超类型构造函数,一次是在创建子类型原型的时候,另一次是在创建子类型原型的时候,另一次是在子类型构造函数内部。
functionParent(age){
this.name=['mike','jack','smith'];
this.age=age;
}
Parent.prototype.run=function(){
returnthis.name+'areboth'+this.age;
}
functionChild(age){
Parent.call(this,age);//给超类型传参,第二次调用
}
Child.prototype=newParent();//原型链继承,第一次调用
vartest1=newChild(21);//写newParent(21)也行
console.log(test1.run());//mike,jack,smithareboth21
vartest2=newChild(22);
console.log(test2.age);
console.log(test1.age);
console.log(test2.run());
//这样可以使test1和test2分别拥有自己的属性age同时又可以有run方法
4.原型式继承:借助原型可以基于已有的对象创建新对象,同时还不必因此创建自定义类型。它要求必须有一个对象可以作为另一个对象的基础。
functionobject(o){
functionF(){};
F.prototype=o;
returnnewF();
}
varperson={
name:'nicho',
friends:['shell','jim','lucy']
}
varanotherPerson=object(person);
anotherPerson.name='Greg';
anotherPerson.friends.push('Rob');
console.log(anotherPerson.friends);//["shell","jim","lucy","Rob"]
varyetAnotherPerson=object(person);
yetAnotherPerson.name='Linda';
yetAnotherPerson.friends.push('Barbie');
console.log(yetAnotherPerson.friends);//["shell","jim","lucy","Rob","Barbie"]
console.log(person.friends);//["shell","jim","lucy","Rob","Barbie"]
ECMAScript5通过新增Object.create()方法规范化了原型式继承,这个方法接收两个参数:一个用作新对象原型的对象和(可选的)一个为新对象定义属性的对象。
varperson2={
name:'nicho',
friends:['shell','jim','lucy']
};
varanoP2=Object.create(person2);
anoP2.name="Greg";
anoP2.friends.push('Rob');
console.log(anoP2.friends);//["shell","jim","lucy","Rob"]
varyetP2=Object.create(person2);
yetP2.name="Linda";
yetP2.friends.push('Barbie');
console.log(yetP2.friends);//["shell","jim","lucy","Rob","Barbie"]
console.log(person2.friends);//["shell","jim","lucy","Rob","Barbie"]
/*以这种方式指定的任何属性都会覆盖原型对象上的同名属性。*/
varthreeP=Object.create(person,{
name:{value:'red'}
});
console.log(threeP.name);//red,如果threeP中无name则输出person2里的name值nicho
5.寄生式继承:思路与寄生构造函数和工厂模式类似,即创建一个仅用于封装继承过程的函数,该函数在内部以某种方式来增强对象,最后再像真地是它做了所有工作一样返回对象。
functionobject(o){
functionF(){};
F.prototype=o;
returnnewF();
};
functioncreateAnother(o){
varcl=object(o);
cl.sayHi=function(){
console.log('hi');
}
returncl;
};
varperson={
name:'nick',
friends:['shelby','court','van']
}
varanotherPerson=createAnother(person);
anotherPerson.sayHi();//hi
console.log(anotherPerson.name);//nick
console.log(anotherPerson.friends);//["shelby","court","van"]
/*这个例子中的代码基于person返回了一个新对象——anotherPerson。新对象不仅具有person
的所有属性和方法,而且还有自己的sayHi()方法*/
寄生组合式继承:无论什么情况下,都会调用两次超类型构造函数,一次是在创建子类型原型的时候,另一次是在创建子类型原型的时候,另一次是在子类型构造函数内部,这样子类型最终会包含超类型对象的全部实例属性,我们不得不在调用子类型构造函数时重写这些属性。因此出现了寄生组合式继承。
6.寄生组合式继承:借用构造函数来继承属性,通过原型链的混成形式来继承方法。基本思路:不必为了指定子类型的原型而调用超类型的构造函数。本质上就是使用寄生式继承来继承超类型的原型,然后再将结果指定给子类型的原型。
functionSuperType(name){
this.name=name;
this.colors=['red','blue','green'];
}
SuperType.prototype.sayName=function(){
console.log(this.name);
}
functionSubType(name,age){
SuperType.call(this,name);
this.age=age;
}
functionobject(o){
functionF(){};
F.prototype=o;
returnnewF();
};
/*inheritPrototype此函数第一步是创建超类型原型的一个副本。第二步是为创建的副本添加constructor属性,
*从而弥补因重写原型而失去的默认的constructor属性,第三步将新创建的对象(副本)赋值给子类型的原型*/
functioninheritPrototype(subType,superType){
varprototype=object(superType.prototype);//创建对象
prototype.constructor=subType;//增强对象
subType.prototype=prototype;//指定对象
}
inheritPrototype(SubType,SuperType);
SubType.prototype.sayAge=function(){
console.log(this.age);
}
varp=newSubType('xiaoli',24);
console.log(p.sayName());
console.log(p.sayAge());
console.log(p.colors)
此方法优点:只调用了一次父类SuperType构造函数,并且因此避免了在SubType.prototype上面创建不必要的多余的属性。同时原型链还能保持不变,还能正常使用instanceof和isPrototypeOf();
以上这篇javascript的几种继承方法介绍就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。