javascript中多继承


js中的继承function classA(sColor){ this.color=sColor; this.sayColor=function() { alert(this.color); };}1、对象冒充function classB(sColor){ this.newMethod=classA; this.newMethod(sColor); delete this.newMethod;}2、call()方法function classB(sColor){ //this.newMethod=classA; //this.newMethod(sColor); //delete this.newMethod; classA.call(this,sColor);}
//////////otherfunction sayColor(sPrefix,sSuffix){ alert(sPrefix+this.color+sSuffix);};var obj=new Object();obj.color="red";sayColor.call(obj,"The color is",",a very nice color indeed.");3、apply()方法function classB(sColor){ //this.newMethod=classA; //this.newMethod(sColor); //delete this.newMethod; classA.apply(this,new Array(sColor));}
function classB(sColor){ //classA.apply(this,new Array(sColor)); classA.apply(this,arguments);}
4、原型链function classA(){}classA.prototype.color="red";classA.prototype.sayColor=function(){ alert(this.color);};function classB(){}classB.prototype=new classA();`
//////////otherfunction sayColor(sPrefix,sSuffix){ alert(sPrefix+this.color+sSuffix);};var obj=new Object();obj.color="red";sayColor.apply(obj,new Array("The color is",",a very nice color indeed."));5、采用冬天原型方法时多继承function Polygon(iSides){ this.sides=iSides; if(typeof Polygon._initialized=="undefined") { Polygon.prototype.getArea=function(){return 0;}; Polygon._initialized=true; }}
function Triangle(iBase,iHeight){ Polygon.call(this,3); this.base=iBase; this.height=iHeight; if(typeof Triangle._initialized=="undefined") { //Triangle.prototype=new Polygon(); //注意上面这句话不能放在类内部,否则会影响基类 Triangle.prototype.getArea=function() { return 0.5 * this.base * this.height; } Triangle._initialized=true; }}
//注意下面这句话只能放在继承类多外面,否则会影响基类Triangle.prototype=new Polygon();
优质内容筛选与推荐>>
1、Android 偏门xml属性
2、数据库的连接
3、IE功能汇总
4、guava文档API制作成chm文件
5、mysql-彻底删除方法


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号