在for循环中关闭/回调 [英] Closure/callback in for loop

查看:181
本文介绍了在for循环中关闭/回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

Javascript循环中的事件处理程序 - 需要关闭吗?


我一直在努力让这个工作一段时间,并决定只问。



我有以下:

  function doSomething(var1,var2){
dojo.xhrGet({
url:somUrl,
content:{value1:var1,value2:var2},
preventCache:true,
handleAs:json,
load:function(response){
for i in response.myObject){
var listItem = new dojox.mobile.ListItem({
label:response.myObject [i] .name,
rightText:response.myObject [i]。 value,
clickable:true,
onClick:function(){
customFunction(response.myObject [i]);
this.transitionTo(someScreen);
}
});
myList.addChild(listItem);
}
},
error:function(e){alert(e);}
});
}
doSomething(myVal1,myVal2);

customFunction(response.myObject [i]); / code>总是返回myObject arrray中的最后一个对象。



有人可以帮助我的语法,所以我可以使这项工作正常吗?我已经阅读关于js关闭和回调,但我只是不能让它工作。



感谢



  onClick:(function(obj){ 
return function(){
customFunction(obj);
this.transitionTo(someScreen);
};
})(response.myObject [i] )

查看此回答说明:


范围是函数级的,而不是块级的,创建
a闭包只是意味着封闭范围被添加到封闭函数的
词法环境中。



循环结束后,函数级变量 i 具有
5 注意:或 内部函数看到。



Possible Duplicate:
Event handlers inside a Javascript loop - need a closure?

I have been trying to get this to work for a while and decided to just ask.

I have the following:

function doSomething(var1, var2) {
    dojo.xhrGet({
        url:"somUrl",
        content: {value1:var1,value2:var2},
        preventCache: true,
        handleAs:"json",
        load: function(response){
            for(var i in response.myObject) {
                var listItem = new dojox.mobile.ListItem({
                    label: response.myObject[i].name,
                    rightText: response.myObject[i].value,
                    clickable: true, 
                    onClick: function() {
                        customFunction(response.myObject[i]);
                        this.transitionTo("someScreen");
                    }   
                });
                myList.addChild(listItem);
            }               
        },
        error:function(e){alert(e);}
    });
}
doSomething(myVal1, myVal2);

The line that says customFunction(response.myObject[i]); always returns the last object in the myObject arrray.

Can someone help me with the syntax so I can make this work correctly? I've been reading about js closures and callbacks but I just can't get it to work.

Thanks

解决方案

You need an additional wrapper function

onClick: (function(obj) {
    return function() {
        customFunction(obj);
        this.transitionTo("someScreen");
    };
})(response.myObject[i])

See this answer for an explanation:

JavaScript's scopes are function-level, not block-level, and creating a closure just means that the enclosing scope gets added to the lexical environment of the enclosed function.

After the loop terminates, the function-level variable i has the value 5 [note: or, in this case, the name of the last property in response.myObject], and that's what the inner function 'sees'.

这篇关于在for循环中关闭/回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆