1
パフォーマンスが向上するベストプラクティスはどれですか?Javascript closure VS dojo lang.hitch
閉鎖またはdojo.lang.hitchを使用していますか?
おかげ実際
パフォーマンスが向上するベストプラクティスはどれですか?Javascript closure VS dojo lang.hitch
閉鎖またはdojo.lang.hitchを使用していますか?
おかげ実際
lang.hitch(scope, method)
、すなわち、それは所与scope
で機能method
を呼び出す機能を返し、クロージャを返します。それはあなたが書くことができるように、オブジェクト指向のコードにコールバックを定義する場合は特に便利です:
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"]);
が、this
内部をcallback
メソッドはbutton
を指します。
詳細については、jsFiddleでhttp://jsfiddle.net/phusick/r7jLr/
thx – jbduzan