JavaScript 中的繼承(上)

          Posted on 2006-11-28 22:51 Jaunt 閱讀(210) 評論(0)  編輯  收藏 所屬分類: JavaScript
          作者: Flyingis
          原載: http://www.aygfsteel.com/flyingis/archive/2006/07/15/58290.html

          ??? 繼承是面向?qū)ο笳Z言基本特征之一,通過繼承可以將父類所具有的特性遺傳到子類。ECMAScript中的繼承不像Java、C++等語言那么明顯,直接通過關(guān)鍵字來實現(xiàn),通常它是通過模擬方式來實現(xiàn)繼承功能的,并且實現(xiàn)方式有多種。

          ??? 在繼承中引入this關(guān)鍵字,使用構(gòu)造器方法定義類來實現(xiàn)繼承。一個構(gòu)造器是一個函數(shù),因此可以將父類的構(gòu)造器作為子類的一個方法使用并進行調(diào)用。

          ? functionClassA(id)? {
          ??
          this .id =
          id;
          ??
          this .sayId =?function()
          {
          ???? alert(
          this
          .id);
          ?? }
          ;
          }


          ?
          function ClassB(id,?name)? {
          ??
          this .newMethod =
          ClassA;
          ??
          this
          .newMethod(id);
          ??
          delete?this.
          newMethod;

          ??
          this .name =
          name;
          ??
          this .sayName =?function()
          {
          ???? alert(
          this
          .name);
          ?? }
          ;
          }

          ??? 注意,子類中所有新的屬性和方法都必需在刪除newMethod后引入,否則,可能存在用父類的屬性和方法重寫子類屬性和方法的危險。另外,使用這種方法還可以實現(xiàn)多重繼承,此時如果兩個父類具有相同的屬性或方法時,最后的類具有優(yōu)先級。由于這種繼承方法比較流行,ECMAScript第三版引入了兩個 Function對象:call()和apply()。

          ??? call()

          ??? call()方法是最接近上述繼承方式的方法,它的第一個參數(shù)是this指向的對象,所有的其他參數(shù)都直接傳到function。

          ? functionsayMessage(first,?last)?{
          ?? alert(first
          +?this.logic +last);
          }
          ;

          varobj =new Object();
          obj.logic
          =?"or";

          sayMessage.call(obj,
          "Coffee ",?"Tea");??//輸出"Coffee?or?Tea"

          ??? 用call()方法來實現(xiàn)繼承,只需要this.newMethod相關(guān)的三行代碼。

          ? functionClassB(id,?name)?{
          ??
          ? //this.newMethod?=?ClassA;
          ??//this.newMethod(id);
          ??//delete?this.newMethod;
          ??ClassA.call(this,?id);??//this指向ClassB的對象

          ??
          this.name =name;
          ??
          this.sayName =?function()?{
          ???? alert(
          this.name);
          ?? }
          ;
          }

          ??? apply()

          ??? apply()方法需要兩個參數(shù):this所指向的對象,和傳到function的由參數(shù)組成的array。

          function?sayMessage(first,?last)??{
          ??alert(first?
          +?this.logic?+last);
          }
          ;

          var?obj?=?new?Object();
          obj.logic?
          =?"or";

          sayMessage.apply(obj,?
          new?Array("Coffee?",?"?Tea"));??//輸出"Coffee?or?Tea"
          ??
          ??? 同樣,使用 apply() 實現(xiàn)繼承可以通過如下方法實現(xiàn)。

          function?ClassB(id,?name)?{
          ??
          //this.newMethod?=?ClassA;
          ??//this.newMethod(id);
          ??//delete?this.newMethod;
          ??ClassA.apply(this,?new?Array(id));??//this指向ClassB的對象

          ??
          this.name?=?name;
          ??
          this.sayName?=?function()?{
          ????alert(
          this.name);
          ??}
          ;
          }

          ??? 當父類構(gòu)造器的參數(shù)和子類構(gòu)造器參數(shù)的順序一致時,可以使用子類的arguments對象作為第二個參數(shù)。否則,必需創(chuàng)建一個array來傳遞參數(shù),或是使用call()方法。

          ??? 文章待續(xù)……
          主站蜘蛛池模板: 屯留县| 陆川县| 华坪县| 荥阳市| 河北省| 灵璧县| 阳东县| 深圳市| 印江| 雅安市| 神农架林区| 千阳县| 永川市| 洞头县| 简阳市| 巴林右旗| 沧州市| 英德市| 汉中市| 启东市| 九寨沟县| 姜堰市| 金溪县| 韩城市| 拉孜县| 本溪| 肥乡县| 闸北区| 建阳市| 烟台市| 青州市| 望奎县| 南安市| 瓦房店市| 和硕县| 百色市| 云霄县| 维西| 宜丰县| 和林格尔县| 高邑县|