JavaScript的寫類方式(5)

來源:互聯網
上載者:User

這篇我們看看各個JS庫的寫類方式,這也是寫類系列的最後一篇。

 

1,Prototype的寫類方式

Prototype中使用Class.create方法,如下

view sourceprint?01 //類名Person 

02 var Person = Class.create(); 

03   

04 //通過原型重寫來定義Person 

05 Person.prototype = { 

06     initialize : function(name) { 

07         this.name = name; 

08     }, 

09     getName : function() { 

10         return this.name; 

11     }, 

12     setName : function(name) { 

13         this.name = name; 

14     }    

15 } 

16   

17 //建立對象 

18 var p = new Person("jack"); 

19 console.log(p.constructor == Person);//false<BR>

initialize完成對象的初始化(相當於建構函式,必不可少),方法依次往下寫即可。

有個問題,但是p.constructor == Person卻為false,這正是Prototype庫一個小缺陷。原因是重寫了Person的原型。為了使constructor能指向正確的構造器,只需在原型重寫時維護好constructor屬性即可。

view sourceprint?01 Person.prototype = { 

02     constructor : Person, //注意這句將修複constructor屬性 

03     initialize : function(name) { 

04         this.name = name; 

05     }, 

06     getName : function() { 

07         return this.name; 

08     }, 

09     setName : function(name) { 

10         this.name = name; 

11     }    

12 }

 


2,Dojo的寫類方式

dojo中用dojo.declare方法來定義一個類。dojo.declare有三個參數
參數1:類名className
參數2:繼承的類superclass
參數3:構造器,方法props

單純的定義一個類實際只需傳第一,三兩個參數。因為這裡只討論如何定義一個類,不討論繼承。

view sourceprint?01 //定義類名 

02 var className = "Person"; 

03 //定義構造器及方法 

04 var proto = { 

05     constructor : function(name){this.name=name;}, 

06     getName : function(){ return this.name;}, 

07     setName : function(name){ this.name = name;} 

08 } 

09   

10 //定義類Person 

11 dojo.declare(className,null,proto); 

12   

13 //建立一個對象 

14 var p = new Person("tom"); 

15 console.log(p.getName());//tom 

16 p.setName("jack"); 

17 console.log(p.getName());//jack 

18   

19 //測試instanceof及p.constructor是否正確指向了Person 

20 console.log(p instanceof Person);//true 

21 console.log(p.constructor === Person);//true

 

3,Ext的寫類方式

Ext中用Ext.extend來定義一個類(當然它更多用來擴充一個類),Ext整個架構各種控制項如Panel,MessageBox等都是用Ext.extend方法來擴充。這裡僅僅用它來定義一個最簡單的類。

這裡只需傳兩個參數即可,第一個參數是根類Object,第二個是原型。比較特殊的是,如果單純的定義一個類,那麼第一個參數永遠傳Object即可。

view sourceprint?01 /** 

02  * Person類 

03  * @param {Object} name 

04  */

05 var Person = Ext.extend(Object,{ 

06     constructor : function(name) {this.name = name;}, 

07     setName : function(name) {this.name = name;}, 

08     getName : function() {return this.name;} 

09 }); 

10   

11 //建立一個對象  

12 var p = new Person("Lily"); 

13 console.log(p.getName());//Lily 

14 p.setName("Andy"); 

15 console.log(p.getName());//Andy 

16   

17 //測試instanceof及p.constructor是否正確指向了Person    

18 console.log(p instanceof Person);//true 

19 console.log(p.constructor == Person);//true

 

4,YUI的寫類方式

view sourceprint?01 //定義包名 

02 YAHOO.namespace("test"); 

03   

04 //定義類 

05 YAHOO.test.Person = function(name) { 

06     this.name = name; 

07 } 

08 YAHOO.test.Person.prototype.setName = function(name){ this.name = name;} 

09 YAHOO.test.Person.prototype.getName = function(){ return this.name;} 

10   

11   

12 //建立一個對象 

13 var p = new YAHOO.test.Person("jack"); 

14   

15 console.log(p.getName());//jack 

16 p.setName(tom); 

17 console.log(p.getName());//tom 

18   

19 //測試instanceof及p.constructor是否正確指向了YAHOO.test.Person    

20 console.log(p instanceof YAHOO.test.Person); 

21 console.log(p.constructor == YAHOO.test.Person);

可以看出除了多了包名,與原始的 建構函式+原型 方式 並無區別。

 

5,Mootools的寫類方式

mootools是被設計成非常緊湊的,模組化的,物件導向的JS庫。mootools中寫類用Class類。匯入後我們寫類時只需要用Class就行了。

view sourceprint?01 /** 

02  * Person類 

03  * @param {Object} name 

04  */

05 var Person = new Class({     

06     initialize: function(name){ 

07         this.name = name; 

08     }, 

09     setName : function(name) { 

10         this.name = name; 

11     }, 

12     getName : function() { 

13         return this.name; 

14     } 

15 }) 

16   

17 //new一個對象 

18 var p = new Person("jack"); 

19   

20 //測試set,get方法 

21 console.log(p.getName());//jac 

22 p.setName(andy); 

23 console.log(p.getName());//andy 

24   

25 //測試instanceof及p.constructor是否正確指向了Person   

26 console.log(p instanceof Person); //true 

27 console.log(p.constructor == Person);  //true

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.