小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

JavaScript 從類繼承

 WindySky 2009-07-02
關(guān)鍵字: javascript設(shè)計(jì)模式

從類繼承

到這里,我們已經(jīng)了解了構(gòu)造函數(shù)和原型對(duì)象如何使您在JavaScript中模擬類。您可以看到,原型鏈可以確保所有對(duì)象都有Object.prototype的公用方法,以及如何使用閉包來(lái)模擬類的私有成員。但這里還缺少點(diǎn)什么。您尚未看到如何從類派生,這在C#中是每天必做的工作。遺憾的是,在JavaScript中從類繼承并非像在C#中鍵入冒號(hào)即可繼承那樣簡(jiǎn)單,它需要進(jìn)行更多操作。另一方面,JavaScript非常靈活,可以有很多從類繼承的方式。

例如,有一個(gè)積累Pet,它有一個(gè)派生類Dog,如圖9所示。這個(gè)在JavaScript中如何實(shí)現(xiàn)呢?Pet類很容易。您已經(jīng)看見(jiàn)如何實(shí)現(xiàn)它了:


Js代碼 復(fù)制代碼
  1. // class Pet   
  2. function Pet(name)   
  3. {   
  4.         this.getName = function()   
  5.         {   
  6.                return name;   
  7.         }   
  8.         this.setName = function(newName)   
  9.         {   
  10.                name = newName;   
  11.         }   
  12. }   
  13.   
  14. Pet.prototype.toString = function()   
  15. {   
  16.         return "This pet's name is: " + this.getName();   
  17. }   
  18. // end of class Pet   
  19.   
  20. var parrotty = new Pet("Parrotty the Parrot");   
  21. alert(parrotty);  

 現(xiàn)在,如何創(chuàng)建從Pet派生的類Dog呢?在圖9種可以看到,Dog有另一個(gè)屬性breed,它改寫了Pet的toString方法(注意,JavaScript的約定是方法和屬性的名稱使用camel大小寫,而不是在C#中建議的Pascal大小寫)。圖10顯示如何這樣做。

 

Figure 10 從 Pet 類派生

Js代碼 復(fù)制代碼
  1. // class Dog : Pet   
  2. // public Dog(String name, String breed)   
  3. function Dog(name, breed)   
  4. {   
  5.         // think Dog: base(name)   
  6.         Pet.call(this, name);   
  7.         this.getBreed = function()   
  8.         {   
  9.                 return breed;   
  10.         }   
  11.            
  12.         // Breed doesn't change, obviously! It's read only.   
  13.         // this.setBreed = function(newBreed){breed = newBreed;}   
  14. }   
  15.   
  16. // this makes Dog.prototype inherits from Pet.prototype   
  17. Dog.prototype = new Pet();   
  18.   
  19. // remember that Pet.prototype.constructor   
  20. // point to Pet. We want out Dog instances' constructor   
  21. // to point to Dog.   
  22. Dog.prototype.constructor = Dog;   
  23.   
  24. // Now we override Pet.prototype.toString   
  25. Dog.prototype.toString = function()   
  26. {   
  27.         return "This dog's name is: " + this.getName() + " , and its breed " +   
  28.         "is: " + this.getBreed();   
  29. };   
  30. //end of class Dog   
  31.   
  32. var dog = new Dog("Buddy""Greed Dane");   
  33. // test the new toStirng()   
  34. alert(dog);   
  35.   
  36. // Testing instanceof (similar to the is operator)   
  37. // (dog is Dog)? yes   
  38. alert(dog instanceof Dog);   
  39. // (dog is Pet)? yes   
  40. alert(dog instanceof Pet);   
  41. // (dog is Object)? yes   
  42. alert(dog instanceof Object);  

 所使用的原型 — 替換技巧正確設(shè)置了原型鏈,因此假如使用C#,測(cè)試的實(shí)例將按預(yù)期運(yùn)行。而且特權(quán)方法仍然會(huì)按預(yù)期運(yùn)行。

下一節(jié):模擬命名空間

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多