2013-11-21 51 views
2

我一直在閱讀有關declare.safeMixin()dojo 1.9文檔,重點介紹它與lang.mixin之間的區別。dojo 1.9:什麼註釋聲明.safeMixin添加?

這裏是我發現的解釋...

safeMixin() is a function defined in dojo/declare. It has the same functionality as dojo/_base/lang::mixin(), but additionally it annotates all copied methods compatibly with dojo/declare. This decoration can affect how this.inherited() works in mixed-in methods.

我可以按照example,但它並沒有真正解釋加入正是何地,任何人都可以給註釋附加了任何進一步的例子到每個複製的方法?

所以要說清楚,我並不是要求繼承的解釋,我只是特別詢問使用declare.safeMixin()而不是lang.mixin添加的註釋。

回答

2
safeMixin

添加nom屬性到在到目標混合功能。該屬性設置爲函數分配給源對象的鍵。例如如果您致電declare.safeMixin(target, { foo: function() {} }),則該函數的nom屬性爲「foo」。這對於this.inherited(arguments)自動計算出它應該調用父項「foo」是必要的。使用safeMixin的替代方法是明確指定父函數的名稱:this.inherited('foo', arguments);

4

使用safeMixin可以將功能混合到一個實例中,該實例可以利用this.inherited的優勢,就像使用declare定義的原型方法可以。

例如,下面將記錄2個消息:

require([ 
    "dojo/_base/lang", 
    "dojo/_base/declare" 
], function(lang, declare){ 
    var A = declare(null, { 
     method: function() { 
      console.log('method in prototype'); 
     } 
    }); 
    var a = new A(); 

    declare.safeMixin(a, { 
     method: function() { 
      this.inherited(arguments); 
      console.log('method in instance'); 
     } 
    }); 

    a.method(); 
}); 

沒有safeMixin,你就不能調用this.inherited(arguments)從上位法(至少,沒有額外的參數) - 你會最終得到一個錯誤:

Error: declare: can't deduce a name to call inherited()