浅谈js中的三种继承方式及其优缺点
第一种,prototype的方式:
//父类
functionperson(){
this.hair='black';
this.eye='black';
this.skin='yellow';
this.view=function(){
returnthis.hair+','+this.eye+','+this.skin;
}
}
//子类
functionman(){
this.feature=['beard','strong'];
}
man.prototype=newperson();
varone=newman();
console.log(one.feature);//['beard','strong']
console.log(one.hair);//black
console.log(one.eye);//black
console.log(one.skin);//yellow
console.log(one.view());//black,black,yellow
这种方式最为简单,只需要让子类的prototype属性值赋值为被继承的一个实例就行了,之后就可以直接使用被继承类的方法了。
prototype属性是啥意思呢?prototype即为原型,每一个对象(由function定义出来)都有一个默认的原型属性,该属性是个对象类型。
并且该默认属性用来实现链的向上攀查。意思就是说,如果某个对象的属性不存在,那么将通过prototype属性所属对象来查找这个属性。如果prototype查找不到呢?
js会自动地找prototype的prototype属性所属对象来查找,这样就通过prototype一直往上索引攀查,直到查找到了该属性或者prototype最后为空(“undefined”);
例如上例中的one.view()方法,js会先在one实例中查找是否有view()方法,因为没有,所以查找man.prototype属性,而prototype的值为person的一个实例,
该实例有view()方法,于是调用成功。
第二种,apply的方式:
//父类
functionperson(){
this.hair='black';
this.eye='black';
this.skin='yellow';
this.view=function(){
returnthis.hair+','+this.eye+','+this.skin;
}
}
//子类
functionman(){
//person.apply(this,newArray());
person.apply(this,[]);
this.feature=['beard','strong'];
}
varone=newman();
console.log(one.feature);//['beard','strong']
console.log(one.hair);//black
console.log(one.eye);//black
console.log(one.skin);//yellow
console.log(one.view());//black,black,yellow
注意:如果apply参数为空,即没有参数传递,则通过newArray()、[]来传递,null无效。
第三种,call+prototype的方式:
//父类
functionperson(){
this.hair='black';
this.eye='black';
this.skin='yellow';
this.view=function(){
returnthis.hair+','+this.eye+','+this.skin;
}
}
//子类
functionman(){
//person.apply(this,newArray());
person.call(this,[]);
this.feature=['beard','strong'];
}
man.prototype=newperson();
varone=newman();
console.log(one.feature);//['beard','strong']
console.log(one.hair);//black
console.log(one.eye);//black
console.log(one.skin);//yellow
console.log(one.view());//black,black,yellow
call方式的实现机制却要多一条man.prototype=newperson();为啥呢?
那是因为call方法只实现了方法的替换而没有作对象属性的复制操作。
googleMapAPI的继承就是使用这种方式。
上面总结了三种继承方式的实现。但是每种方法都有其优缺点。
假如父类是这样的:
//父类
functionperson(hair,eye,skin){
this.hair=hair;
this.eye=eye;
this.skin=skin;
this.view=function(){
returnthis.hair+','+this.eye+','+this.skin;
}
}
子类应该如何设计,使子类man在创建对象的同时传递参数到父类person,prototype的继承方式就不适用了,
必须采用apply或者call的方式了:
//apply方式
//子类
functionman(hair,eye,skin){
person.apply(this,[hair,eye,skin]);
this.feature=['beard','strong'];
}
//call方式
//子类
functionman(hair,eye,skin){
person.call(this,hair,eye,skin);
this.feature=['beard','strong'];
}
但是用apply方法也还是有缺点的,为什么?在js中,我们有个非常重要的运算符就是”instanceof”,该运算符用来比较某个对向是否为某种类型。
对于这个例子,one实例除了是man类型,也应该是person类型,但是apply方式继承之后,one却不属于person类型,即(oneinstanceofperson)的值为false。
经此种种,最好的继承方式就是call+prototype方式了,之后你可以试一下(oneinstanceofBaseClass)的值是否为true。
第三种继承方式也有缺陷:子类new对象时要传一遍父类所需的参数,而且会重现父类中的属性和方法,下面这种继承方式才是完善的:
functionPerson(name){
this.name=name;
}
Person.prototype.getName=function(){
returnthis.name;
}
functionChinese(name,nation){
Person.call(this,name);
this.nation=nation;
}
//继承方法
functioninherit(subClass,superClass){
functionF(){}
F.prototype=superClass.prototype;
subClass.prototype=newF();
subClass.prototype.constructor=subClass.constructor;
}
inherit(Chinese,Person);
Chinese.prototype.getNation=function(){
returnthis.nation;
};
varp=newPerson('shijun');
varc=newChinese("liyatang","China");
console.log(p);//Person{name:"shijun",getName:function}
console.log(c);//Chinese{name:"liyatang",nation:"China",constructor:function,getNation:function,getName:function}
console.log(p.constructor);//functionPerson(name){}
console.log(c.constructor);//functionChinese(){}
console.log(cinstanceofChinese);//true
console.log(cinstanceofPerson);//true
以上这篇浅谈js中的三种继承方式及其优缺点就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。