javascript 繼承實現方法


Google

<script type="text/javascript"> google_ad_client = "pub-5033576919944123"; google_ad_width = 728; google_ad_height = 90; google_ad_format = "728x90_as"; google_ad_type = "text_image"; //2007-10-24: csdn.blog google_ad_channel = "8548491739"; </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
      javascript的繼承機制並不是明確規定的,而是通過模仿實現的,意味着繼承不是由解釋程序處理,開發者有權決定最適合的繼承方式. 下面我給出幾種常用的方法:
     1 .對象冒充
          原理: 構造函數使用this關鍵字給所有屬性和方法賦值, 因爲構造函數只是一個函數,所以可以使ClassA的構造函數成爲classB的方法,然後調用它.這樣classB就會收到classA的構造函數中定義的屬性和方法.例子:
   function classA(name)
   {
         this.name=name;
         this.showName=function(){alert(this.name);}
   }
   function classB(name)
   {
         this.newMethod = classA;
         this.newMethod(name);
   }
   obj = new classA("hero");
   objB = new classB("dby");
   obj.showName(); //   print hero
   objB.showName(); // print dby  說明classB 繼承了classA的方法.
   對象冒充可以實現多重繼承  例如
   function classz(){
   this.newMethod = classX;
   this.newMethod();
   delete this.newMethod;
   this.newMethod=classY;
   this.newMethod():
   delete this.newMethod;
  }
  但是如果classX和classY有相同的屬性或者方法,classY具有高優先級.
   2.call()方法
    call方法使與經典的對象冒充法就相近的方法,它的第一個參數用作this的對象,其他參數都直接傳遞給函數自身.
       function sayName(perfix)
   {
  alert(perfix+this.name);
   }
   obj= new Object();
   obj.name="hero";
   sayName.call(obj,"hello," );
   function classA(name)
   {
         this.name=name;
         this.showName=function(){alert(this.name);};
   }
   function classB(name)
   {
  classA.call(this,name);
   }
   objB = new classB("bing");
   objB.showName();////說明classB繼承classA的showName方法
  3.apply()方法
   
aplly()方法有2個參數,一個用作this對象,一個使傳遞給函數的參數數組.
       function sayName(perfix)
   {
  alert(perfix+this.name);
   }
   obj= new Object();
   obj.name="hero";
   sayName.aplly(obj,new Array("hello,") );
  4. 原型鏈
   prototype對象的任何屬性和方法都會被傳遞給對應類的所有實例,原型鏈就是用這種方式來顯現繼承.
    function classA (){}
    classA.prototype.name="hero";
    classA.prototype.showName=function(){alert(this.name)}
    function classB(){}
   classB.prototype=new classA();
   objb = new classB()
   objb.showName();//print hero  說明b繼承了a的方法
   這裏需要注意 調用classA的構造函數時,沒有給它傳遞參數,這是原型鏈的標準做法,確保函數的構造函數沒有任何參數.
   並且 子類的所有屬性和方法,必須出現在prototype屬性被賦值後,應爲在它之前賦的值會被刪除.因爲對象的prototype屬性被替換成了新對象,添加了新方法的原始對象將被銷燬.
  
   5 混和方式
       就是用冒充方式 定義構造函數屬性,用原型法定義對象方法.
   function classA(name)
   {
         this.name=name;
   }
   classA.prototype.showName=function(){alert(this.name)}
   function classB(name)
   {
  classA.call(this,name);
   }
   classB.prototype = new classA();
   classB.prototype.showName1=function(){alert(this.name+"*****");};
   obj = new classB("hero");
   obj.showName();
   obj.showName1();
  在classB的構造函數中通過調用call方法 繼承classA中的name屬性,用原型鏈來繼承classA的showName方法.
 

<script type="text/javascript"> google_ad_client = "pub-5033576919944123"; google_ad_width = 728; google_ad_height = 90; google_ad_format = "728x90_as"; google_ad_type = "text_image"; //2007-10-24: csdn.blog google_ad_channel = "8548491739"; </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章