2012-05-11 55 views

回答

3

其實lang.hitch(scope, method)返回一個封閉的,即它返回一個函數,它會調用函數在給定的scopemethod。這定義在面向對象代碼的回調尤其是當是非常有用的,所以你可以寫:

on(dom.byId("button"), "click", lang.hitch(this, "callback")); 

代替:

on(dom.byId("button"), "click", function(scope, method) { 
    return function() { 
     method.apply(scope); 
    } 
}(this, this["callback"])); // execute the anonymous function immediately to get a closure 

像這樣將工作:

on(dom.byId("button"), "click", this["callback"]); 

thiscallback方法將指向button

見額外細節的jsfiddle的完整代碼:http://jsfiddle.net/phusick/r7jLr/

+0

THX你的答案 – jbduzan