dojo.hitch是一个比较有用的方法,主要是给一个函数提供执行环境。
var theAccumulator = {
total: 0,
clear: function(){
this.total = 0;
},
add:function(x){
this.total += x;
},
getResult: function(){
return this.total;
}
}
function printResult(f){
console.log("result = :" + f());
}
theAccumulator.clear();
theAccumulator.add(100);
theAccumulator.add(200);
printResult(theAccumulator.getResult);
//运行结果
//这样执行的时候就返回undefined了
//
printResult(dojo.hitch(theAccumulator,"getResult"));