JavaScript的寫類方式(4)——轉

來源:互聯網
上載者:User

轉自:http://www.cnblogs.com/snandy/archive/2011/03/07/1973241.html

 

工具函數如下

01 /**
02  * $class 寫類工具函數之三
03  * @param {String} className
04  * @param {Function} superClass
05  * @param {Function} classImp
06  */
07 function $class(className, superClass, classImp){
08     if(superClass === "") superClass = Object;
09   
10     function clazz(){
11         if(typeof this.init == "function"){
12             this.init.apply(this, arguments);
13         }
14     }
15   
16     var p = clazz.prototype = new superClass();
17     var _super = superClass.prototype;
18     window[className] = clazz;
19     classImp.apply(p, [_super]);
20 }

 

定義一個Person類

01 $class('Person', '', function(){
02     // 建構函式
03     this.init = function(name){
04         this.name = name;
05     };
06     // 方法體
07     this.getName = function(){
08         return this.name;
09     };
10     this.setName = function(name){
11         this.name = name;
12     };
13 });

建立對象看看

1 var p = new Person('Jack');
2 console.log(p); 
3 console.log(p.constructor == Person); // false     

使用該工具函數寫類需注意,this.init方法必不可少。使用過Prototype庫的同學會知道Class.create後的initialize方法也是必不可少的。

因為沒考慮繼承,第二個參數superClass使用Null 字元串,即預設繼承於Object。

從輸出為false可看到,這個工具類沒有去維護constructor屬性。 設計的每一種寫類方式都是有取捨的,這完全取決於設計者的意圖。

 

工具函數如下

01 /**
02  * $class 寫類工具函數之三
03  * @param {String} className
04  * @param {Function} superClass
05  * @param {Function} classImp
06  */
07 function $class(className, superClass, classImp){
08     if(superClass === "") superClass = Object;
09   
10     function clazz(){
11         if(typeof this.init == "function"){
12             this.init.apply(this, arguments);
13         }
14     }
15   
16     var p = clazz.prototype = new superClass();
17     var _super = superClass.prototype;
18     window[className] = clazz;
19     classImp.apply(p, [_super]);
20 }

 

定義一個Person類

01 $class('Person', '', function(){
02     // 建構函式
03     this.init = function(name){
04         this.name = name;
05     };
06     // 方法體
07     this.getName = function(){
08         return this.name;
09     };
10     this.setName = function(name){
11         this.name = name;
12     };
13 });

建立對象看看

1 var p = new Person('Jack');
2 console.log(p); 
3 console.log(p.constructor == Person); // false     

使用該工具函數寫類需注意,this.init方法必不可少。使用過Prototype庫的同學會知道Class.create後的initialize方法也是必不可少的。

因為沒考慮繼承,第二個參數superClass使用Null 字元串,即預設繼承於Object。

從輸出為false可看到,這個工具類沒有去維護constructor屬性。 設計的每一種寫類方式都是有取捨的,這完全取決於設計者的意圖。

相關文章

聯繫我們

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