JavaScript 中的继承(上)

    作者:Flyingis

    继承是面向对象语言基本特征之一,通过继承可以将父类所具有的特性遗传到子类。ECMAScript中的继承不像Java、C++等语言那么明显,直接通过关键字来实现,通常它是通过模拟方式来实现继承功能的,并且实现方式有多种。

    在继承中引入this关键字,使用构造器方法定义类来实现继承。一个构造器是一个函数,因此可以将父类的构造器作为子类的一个方法使用并进行调用。

ExpandedBlockStart.gif ContractedBlock.gif function ClassA(id) dot.gif {
InBlock.gif  
this .id = id;
ExpandedSubBlockStart.gifContractedSubBlock.gif  
this .sayId = function() dot.gif{
InBlock.gif     alert(
this.id);
ExpandedSubBlockEnd.gif   }
;
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
function ClassB(id, name) dot.gif {
InBlock.gif  
this .newMethod = ClassA;
InBlock.gif  
this .newMethod(id);
InBlock.gif  
delete this.newMethod;
InBlock.gif
InBlock.gif  
this.name= name;
ExpandedSubBlockStart.gifContractedSubBlock.gif  
this.sayName= function()dot.gif{
InBlock.gif     alert(
this.name);
ExpandedSubBlockEnd.gif   }
;
ExpandedBlockEnd.gif}

    注意,子类中所有新的属性和方法都必需在删除newMethod后引入,否则,可能存在用父类的属性和方法重写子类属性和方法的危险。另外,使用这种方法还可以实现多重继承,此时如果两个父类具有相同的属性或方法时,最后的类具有优先级。由于这种继承方法比较流行,ECMAScript第三版引入了两个Function对象:call()和apply()。

    call()

    call()方法是最接近上述继承方式的方法,它的第一个参数是this指向的对象,所有的其他参数都直接传到function。

ExpandedBlockStart.gif ContractedBlock.gif function sayMessage(first, last)  {
InBlock.gif   alert(first
+ this.logic +last);
ExpandedBlockEnd.gif}
;
None.gif
None.gif
var obj = new Object();
None.gif obj.logic
=  " or " ;
None.gif
None.gif sayMessage.call(obj,
" Coffee " " Tea " );   // 输出"Coffee or Tea"

    用call()方法来实现继承,只需要this.newMethod相关的三行代码。

ExpandedBlockStart.gif ContractedBlock.gif function ClassB(id, name) dot.gif {
InBlock.gif  
  //this.newMethod = ClassA;
InBlock.gif
  //this.newMethod(id);
InBlock.gif
  //delete this.newMethod;
InBlock.gif
  ClassA.call(this, id);  //this指向ClassB的对象
InBlock.gif

InBlock.gif  
this.name =name;
ExpandedSubBlockStart.gifContractedSubBlock.gif  
this.sayName = function() {
InBlock.gif     alert(
this.name);
ExpandedSubBlockEnd.gif   }
;
ExpandedBlockEnd.gif}

    apply()

    apply()方法需要两个参数:this所指向的对象,和传到function的由参数组成的array。

ExpandedBlockStart.gif ContractedBlock.gif function  sayMessage(first, last)   dot.gif {
InBlock.gif  alert(first 
+ this.logic +last);
ExpandedBlockEnd.gif}
;
None.gif
None.gif
var  obj  =   new  Object();
None.gifobj.logic 
=   " or " ;
None.gif
None.gifsayMessage.apply(obj, 
new  Array( " Coffee  " "  Tea " ));   // 输出"Coffee or Tea"
  
    同样,使用 apply() 实现继承可以通过如下方法实现。

ExpandedBlockStart.gif ContractedBlock.gif function  ClassB(id, name)  dot.gif {
InBlock.gif  
//this.newMethod = ClassA;
InBlock.gif
  //this.newMethod(id);
InBlock.gif
  //delete this.newMethod;
InBlock.gif
  ClassA.apply(thisnew Array(id));  //this指向ClassB的对象
InBlock.gif

InBlock.gif  
this.name = name;
ExpandedSubBlockStart.gifContractedSubBlock.gif  
this.sayName = function() dot.gif{
InBlock.gif    alert(
this.name);
ExpandedSubBlockEnd.gif  }
;
ExpandedBlockEnd.gif}

    当父类构造器的参数和子类构造器参数的顺序一致时,可以使用子类的arguments对象作为第二个参数。否则,必需创建一个array来传递参数,或是使用call()方法。

    文章待续……
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值