国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > JS > 正文

Javascript繼承機制詳解

2024-05-06 16:37:34
字體:
來源:轉載
供稿:網友

學完了Javascript類和對象的創建之后,現在總結一下Javascript繼承機制的實現。Javascript并不像Java那樣對繼承機制有嚴格明確的定義,它的實現方式正如它的變量的使用方式那樣也是十分寬松的,你可以設計自己的方法“模仿”繼承機制的實現。有以下幾種方法:

1、對象冒充

 <script type="text/javascript">   function classA(str){     this.str=str;     this.printstr=function(){       document.write(this.str);       document.write("<br>");     }     this.getstr=function(){       return this.str;     }       }   function classB(name,str){     //下面這兩句代碼相當于將classA代碼體中的內容搬到這里     this.newMethod1=classA;     this.newMethod1(str);     //注意,這里的寫法     delete this.newMethod1;     //新的方法和屬性的定義須在刪除了newMethod之后定義,因為可能覆蓋超類的屬性和方法。     this.name=name;     this.sayName=function(){       document.write(this.name);       document.write("<br>");     }        }   var a=new classB("Amy","helloworld");   a.printstr();   alert(a.getstr());   a.sayName(); </script>

function定義的代碼塊就相當于一個類,你可以用而且它有this關鍵字,你可以用this為它添加屬性和方法,上述代碼中有以下兩句:

this.newMethod1=classA;
 this.newMethod1(str);

classB中定義了newMethod1變量,它是一個引用,指向了classA,并且還調用了classA,這兩句代碼的作用等同于直接將classA代碼塊中的內容直接復制到這里,這樣創建的classB對像當然具有classA的屬性和方法了。對象冒充還可以實現多繼承,如下:

function ClassZ() { this.newMethod = ClassX; this.newMethod(); delete this.newMethod;this.newMethod = ClassY; this.newMethod(); delete this.newMethod;}

不過,classY會覆蓋classX中同名的屬性和方法,如果設計沒問題的話,classz也不應該繼承具有相同屬性和方法的不同類。

2、利用call()方法

 <script type="text/javascript">   function classA(str){     this.str=str;     this.printstr=function(){       document.write(this.str);       document.write("<br>");     }     this.getstr=function(){       return this.str;     }       }   function classB(name,str){   //利用call方法實現繼承     classA.call(this,str);     this.name=name;     this.sayName=function(){       document.write(this.name);       document.write("<br>");     }        }   var a=new classB("Amy","helloworld");   a.printstr();   alert(a.getstr());   a.sayName(); </script>

call()方法中第一個參數傳遞一個對象,這里的this指的是當前對象,后面的參數(可能有多個)是指傳遞給調用call()方法的類(函數)所需要的參數,classA.call()也是相當于直接將classA代碼塊中的內容直接復制到這里,classB的對象同樣可以直接使用classB中的變量和方法。

3、原型鏈

 <script type="text/javascript">   function cA(){};   cA.prototype.name="John";   cA.prototype.sayName=function(){     document.write(this.name);     document.write("<br>");   }   function cB(){};   cB.prototype=new cA();   cB.prototype.age=23;   cB.prototype.sayAge=function(){     document.write(this.age);     document.write("<br>");   }   var objB=new cB();   objB.sayAge();   objB.sayName();   document.write("is objB the instance of cA "+(objB instanceof cA));   document.write("<br>");   document.write("is objB the instance of cB "+(objB instanceof cB));   document.write("<br>"); </script>

這里對類的定義要用prototype關鍵字,定義function時不帶有參數,prototype后面的變量或方法相當于java中被static修飾后的屬性和方法,是屬于所有對象的,這里有個特殊之處:cB.prototype=new cA();該句話相當于將cA對象內容復制給cB,cB還可以追加自己的屬性和方法。

4、混合方法

 <script type="text/javascript">   function cA(name){     this.name=name;   };   cA.prototype.sayName=function(){     document.write(this.name);     document.write("<br>");   }   function cB(name,age){     cA.call(this,name);     this.age=age;   };   cB.prototype=new cA();   cB.prototype.sayAge=function(){     document.write(this.age);     document.write("<br>");   }   var objB=new cB("Alan",27);   objB.sayName();   objB.sayAge();   document.write("is objB the instance of cA "+(objB instanceof cA));   document.write("<br>");   document.write("is objB the instance of cB "+(objB instanceof cB));   document.write("<br>"); </script>

這里可以將屬性封裝在類體內,而方法利用原型方式定義,個人感覺,這是一個很好的設計方法,利用prototype定義的函數可以為多個對象重用,這里需要注意兩點:cB類體內有cA.call(this,name);同時還要將cB原型賦為cB對象,即:cB.prototype=new cA();cA.call(this,name)同樣相當于將cA類塊內的代碼復制于此,后面一句話又將cA的方法添加給cB,同時cB還可以追加自己的屬性和方法。

以上是本次對Javascript繼承機制的總結,不足之處望各位指正批評。


注:相關教程知識閱讀請移步到JavaScript/Ajax教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南江县| 绥宁县| 新建县| 旌德县| 醴陵市| 调兵山市| 巴彦淖尔市| 潼南县| 伊川县| 长乐市| 仁布县| 奉贤区| 永济市| 新宁县| 田林县| 平阳县| 日照市| 丰顺县| 温宿县| 新平| 绵竹市| 津市市| 来凤县| 正镶白旗| 咸宁市| 宜都市| 土默特右旗| 两当县| 太原市| 古田县| 海口市| 奉新县| 潼关县| 瑞安市| 新闻| 桦南县| 化隆| 阜新市| 金堂县| 怀柔区| 石楼县|