dojo之面向對象編程

Dojo
作為一個強大的javascript工具箱,有它自己面向對象的開發方式,用declare解決了對象的創建和繼承的問題,文檔中的例子: 
dojo.declare("my.classes.bar", my.classes.foo, {  
        
// properties to be added to the class prototype  
        someValue: 2,  
        
// initialization function  
        constructor: function(){  
            
this.myComplicatedObject = new ReallyComplicatedObject();  
        }
,  
        
// other functions  
        someMethod: function(){  
            doStuff();  
        }
  
    }

);   
 
declare的第一個參數是對象名稱,最后一個參數指定在這個對象里要添加的內容,包括函數和屬性,寫個例子
dojo.declare("Apple"null{  
        price: 
5,  
        constructor: 
function(weight) {  
            
this.total = weight * this.price;  
        }
,  
        print: 
function() {  
            alert(
"The total price is " + this.total);  
        }
  
    }
  
);  
var myapple = new Apple(10);  
myapple.print();    
//輸出結果:"The total price is 50"  

上例通過declare創建了一個Apple對象,javascript本身沒有類的概念,可以使用對象本身來創建新的對象myapple,通過構造函數的參數值計算蘋果的總價,print函數輸出結果,非常形象的構建了一個Apple“類”,非常容易理解。要注意的是,這里如果聲明默認構造函數, "new Apple(10)"將直接執行默認構造函數,帶參數的構造函數就被忽略了,并非C++中順序執行。

    注意dojo.declare第二個參數,如果創建一個獨立的新對象,可以設為null,當需要從其他一個或多個對象繼承時,則為對象名稱,這樣就方便的實現了對象繼承。多個對象繼承,declare第二個參數為一數組,第一個元素為原型父對象,其他的為mixin對象,通過代碼來理解。
<script>  
    dojo.declare(
"Apple"null{  
            price : 
5,  
            constructor : 
function(weight) {  
                
this.total = weight * this.price;  
            }
,  
//          constructor : function() {  
//
              alert("Create Apple !");  
//
          },  
            print : function() {  
                alert(
"The total price is " + this.total);  
            }
  
        }

    );  
    dojo.declare(
"AppleTree"null{  
            constructor : 
function() {  
                alert(
"Create AppleTree !");  
            }
,  
            print : 
function() {  
                alert(
"This is an apple tree");  
            }
,  
            additional : 
function() {  
                alert(
"This is a mixin class");  
            }
  
        }

    );  
    dojo.declare(
"GreenApple", [Apple, AppleTree], {  
            constructor : 
function() {  
                alert(
"Getting a green apple");  
            }
  
        }

    );  
</script>

創建一個GreenApple對象,測試alert執行順序!mixin對象的方法將覆蓋之前對象中的同名函數,除非子對象也聲明了同名函數print。
//輸出  
//
"The height of the tree is #ff0000"  
//
"Getting a green apple"  
var gapple = new GreenApple();  
//輸出,覆蓋了Apple對象的print  
//
"This is an apple tree"  
gapple.print();  
//"This is a mixin class"  
gapple.additional();  

dojo
/_base/_loader/bootstrap.js有專門的mixin函數,用于對象的拷貝,將一個創建好的對象拷貝到新的對象中
var copy = dojo.mixin({}new Apple(2));  
copy.print(); 

print輸出結果是"The total price is 10",mixin參數一定是創建好的對象實例,否則出錯!dojo.extend則可以將一個或多個對象的屬性、方法拷貝到一個原型上,通過prototype實現繼承,這是繼承的另外一種方式。

    通過declare、mixin、extend,dojo給我們提供了一種方便的對象創建與擴展機制,一般情況下夠用了,感覺還是比較方便,使用時也存在一些限制,翻翻源代碼就能理解。這里主要是要知道dojo是如何面向對象的,方便我們更好的理解dojo基礎功能,及dijit和dojox,dojo最為強大還是它的widgets。本文涉及的js源碼:
mixin:dojo/_base/_loader/bootstrap.js
extend:dojo/_base/lang.js
declare:dojo/_base/declare.js