zl程序教程

您现在的位置是:首页 >  后端

当前栏目

js继承的实现代码

2023-06-13 09:14:24 时间
base.js--继承的实现==========================
【注】:继承后,如果父类是一个类,则会继承其属性,方法(包括用prototype声明的),静态方法,否则只有属性和方法。
复制代码代码如下:

Object.prototype.extendf=function(a,b){
if(!a||!b)return;
varfa=typeofa=="function";
varfb=typeofb=="function";
varcha=function(a,b){
for(varcinb){
if(a[c]==undefined)//子类重写
a[c]=b[c];
}
returna;//返回继承后的对象
}
if(fa&&fb){
b.apply(this,a.arguments);
cha(a,b);
this["base"]=newb;//通过base访问父类
returncha(this,b.prototype);
}
elseif(!fa&&fb){
cha(a,newb);
a["base"]=newb;
returncha(a,b);
}elseif(fa&&!fb){
cha(a,b);
this["base"]=b;
returncha(this,b);
}elseif(!fa&&!fb){
a["base"]=b;
returncha(a,b);
}
}

测试页:用法
复制代码代码如下:

<html>
<head>
<scripttype="text/javascript"src="base.js"></script>
<scripttype="text/javascript">
varcar2={
name:"轿车【父类】",
price:"几万【父类】",
start:function(){
alert(this.name+"已启动2!【父类】");
},
run:function(){
alert(this.name+"在行驶当中2。。。【父类】");
},
stop:function(){
alert(this.name+"已停止2!【父类】");
},
remark:function(){return"【父类】2我是一辆"+this.name+";价值"+this.price;}
//this.remark="我是一辆"+this.name+";价值"+this.price;
}
//car2.prototype.extra=function(ext){
//returnthis.name+"的关税2是:"+ext;
//}
car2.protect="【父类】2保护的";
car2.noExtra=function(){
returncar.protect+"不交关税2【父类】";
}
varcar=function(name,price){
this.name=name||"轿车[父类]";
this.price=price||"几万[父类]";
this.start=function(){
alert(this.name+"已启动![父类]");
};
this.run=function(){
alert(this.name+"在行驶当中。。。[父类]");
};
this.stop=function(){
alert(this.name+"已停止![父类]");
};
this.remark=function(){return"[父类]我是一辆"+this.name+";价值"+this.price;};
//this.remark="我是一辆"+this.name+";价值"+this.price;//注意,这样做name和price将得不到传参,故注释
}
car.prototype.extra=function(ext){
returnthis.name+"的关税是[父类]:"+ext;
}
car.protect="[父类]保护的";
car.noExtra=function(){
returncar.protect+"不交关税[父类]";
}
varBMW=function(){
this.extendf(BMW,car);
this.name="BMW【子类】";
this.start=function(){
alert(this.name+"专属启动装置!");
};
return("this.name1="+this.name);
}
varBMW2=function(){
this.extendf(BMW2,car2);
this.name="宝马终极2号【子类】";
this.start=function(){
alert(this.name+"专属启动装置2号未来!");
};
return("this.name1="+this.name);
}
varbensi={
name:"bensi",
price:"130万",
start:function(){
alert(this.name+"华丽启动!");
},
stop:function(){
alert(this.name+"专用刹车停止!");
}
}
bensi.noExtra=function(){
return"谁敢收税?";
}
varautuo={
name:"autuo【子类】",
price:"1万",
stop:function(){
alert(this.name+"奥拓失灵了!");
}
}
functionChangAn(){
this.extendf(ChangAn,car);
//this.name="CHANGAN【子类】";
this.run=function(){
alert(this.name+"走的有点慢。。。");
}
}
varftest=function(){
vartb=newBMW("宝马","70万");
testRun(tb);
alert(BMW.noExtra());
}
varftest2=function(){
vartb=bensi//("奔驰","120万");
tb.extendf(bensi,car);
testRun(bensi);
alert(bensi.noExtra());
}
varftest3=function(){
vartb=newChangAn("长安[传参]","5万");
testRun(tb);
alert(ChangAn.noExtra());
}
varftest4=function(){
vartb=autuo
tb.extendf(autuo,car2);
testRun(tb);
alert(autuo.noExtra());
}
varftest5=function(){
vartb=autuo
tb.extendf(autuo,bensi);
alert(tb.name);
tb.start();
tb.stop();
alert(autuo.noExtra());
}
varftest6=function(){
vartb=newBMW2("宝马2号","65万");
varscar=document.getElementById("showcar");
scar.innerHTML=tb.remark();
alert(tb.name);
tb.start();
tb.stop();
alert(BMW2.noExtra());
}
//测试输出
functiontestRun(tb){
varscar=document.getElementById("showcar");
if(!scar)returnfalse;
scar.innerHTML=tb.remark();
tb.base.start();
tb.start();
tb.base.run();
tb.run();
tb.base.stop();
tb.stop();
alert(tb.extra("1万"));//父类为Object时这个会出错,因为父类本身就没有
}
</script>
</head>
<body>
js测试:
<inputtype="button"value="宝马"onclick="ftest()">
<inputtype="button"value="奔驰"onclick="ftest2()">
<inputtype="button"value="长安"onclick="ftest3()">
<inputtype="button"value="奥拓"onclick="ftest4()">
<inputtype="button"value="奔驰类的奥拓"onclick="ftest5()">
<inputtype="button"value="宝马2号"onclick="ftest6()">
<divid="showcar"></div>
</body>
</html>

ps:没有注意到性能问题,往大家改善
想只用一个参数,不知道大家有没有办法?
嵌套类没试过。