Javascript中的類的建立

來源:互聯網
上載者:User
  1. /**  
  2.  * Factory 方法:  
  3.  * 存在問題:重複建立對象eat  
  4.  */  
  5. function CreatePeople(name){   
  6.   var  people=new Object();   
  7.   people.name=name;   
  8.   people.eat=function(){   
  9.     alert(this.name+' is eating  !!')   
  10.   };   
  11.   return people;   
  12. }   
  13. /**  
  14.  * 建構函式法:  
  15.  * 存在問題:重複建立對象eat  
  16.  * 使用new 來建立對象,如:var p=new People('saber')  
  17.  * 在執行第一句代碼前,先建立一個對象(並返回,所以建構函式最後無須return),只有用this才能訪問該對象,  
  18.  *   
  19.  */  
  20. function People(name){   
  21.   this.name=name;   
  22.   this.eat=function(){   
  23.     alert(this.name+' is eating  !!')   
  24.   };   
  25. }   
  26. /**  
  27.  * 混合的建構函式/原型方式(推薦)  
  28.  */  
  29. var People1=function (name){   
  30.     this.name=name;    
  31. }   
  32. People1.prototype={   
  33.     eat:function(){   
  34.         alert(this.name+' is eating !')   
  35.     }   
  36. }   
  37. /* 自訂建構函式為:initialize  
  38.  * 在使用new建立對象的時候,執行initialize函數,  
  39.  */  
  40. var People11=function (){   
  41.     this.initialize.apply(this, arguments);   
  42. }   
  43. People11.prototype={   
  44.     initialize:function(name){   
  45.         this.name=name;   
  46.         alert(' Initializing Ok !!');   
  47.     },   
  48.     eat:function(){   
  49.         alert(this.name+' is eating !')   
  50.     }   
  51. }   
  52. /* 有以上基礎,如果我們要以一種統一的方式來定義類,該如何呢?  
  53.  * 像java中定義類都採用Class關鍵字一樣,而在JS中,沒有類這個概念,  
  54.  * 那麼我們首先聲明一個對象MyClass  
  55.  */  
  56.   
  57. var MyClass=function(){   
  58.     return function(){//閉包   
  59.         this.initialize.apply(this, arguments);   
  60.     }   
  61. }   
  62. /*  
  63.  * 定義類:  
  64.  */  
  65. var ClassOne=MyClass();   
  66. ClassOne.prototype={   
  67.     initialize:function(name){   
  68.         this.name=name;    
  69.         alert(this.name+' initializing Ok !')   
  70.     },   
  71.     methodOne:function(){   
  72.         alert("My name is:" + this.name);   
  73.     }   
  74. }   
  75.   
  76. var ClassTwo=MyClass();   
  77. ClassTwo.prototype={   
  78.     initialize:function(name){   
  79.         this.name=name;        
  80.     },   
  81.     methodTwo:function(){   
  82.         alert("My name is:" + this.name);   
  83.     }   
  84. }   
  85.   
  86. /**  
  87.  * 採用prototype.js,它的方法和我們上邊的一樣:  
  88.  * var Class = {  
  89.  * create: function() {  
  90.  *  return function() {  
  91.  *    this.initialize.apply(this, arguments);  
  92.  *  }  
  93.  * }  
  94.  *}  
  95.  */  
  96. var People2=Class.create();   
  97. People2.prototype={   
  98.     //自訂的建構函式:initialize   
  99.     initialize:function(name,sex){   
  100.         this.name=name;   
  101.         this.sex=sex;   
  102.     },   
  103.     eat:function(){   
  104.         alert(this.name+' is eating ');   
  105.     },   
  106.     showSex:function (){   
  107.         alert(this.name+'\'s sex is :'+this.sex);   
  108.     }   
  109. }  
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.