javascript继承

用call方法实现继承
function classA(sColor){ 
       this.color=sColor; 
       this.sayColor=function(){ 
         alert(this.color); 
       } 


function classC(sColor,sName){ 
    classA.call(this,sColor); 
    this.name=sName; 
    this.sayName=function(){ 
      alert(this.name); 
    } 

var obj1=new classC("red","jack"); 
obj1.sayColor(); 
obj1.sayName();

 

用apply方法实现继承

function classD(sColor,sName){ 
   classA.apply(this,new Array(sColor)); 
   this.name=sName; 
   this.sayName=function(){ 
     alert(this.name); 
   } 
 } 
  
 var obj2=new classD("blue","sherry"); 

obj2.sayColor(); 
obj2.sayName();

 

构造函数,原型混合方法

  function classA(sName){ 
    this.name=sName; 
  } 
   
  classA.prototype.sayName=function(){ 
    alert(this.name); 
  } 
   
  function classB(sName,sHeight){ 
     classA.call(this,sName) 
     this.height=sHeight; 
  } 
   
  classB.prototype=new classA(); 
   
  classB.prototype.sayHeight=function(){ 
     alert(this.height); 
  } 
   
  var obj3=new classA("jack"); 
   
  obj3.sayName(); 
   
  var obj4=new classB("shrry",27); 
   
  obj4.sayName(); 
   
  obj4.sayHeight();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值