admin管理员组文章数量:1252717
I want to add some extra parameters to the Google geocoder API call as I'm running it in a loop, but am not sure how to append closure parameters to their anonymous function that already has default parameters that are passed in by the call to the API.
For example:
for(var i = 0; i < 5; i++) {
geocoder.geocode({'address': address}, function(results, status) {
// Geocoder stuff here
});
}
I want to be able to use the value of i in the passed geocoder.geocode() anonymous function, but if I had a closure using }(i));
on line 4 for example that would replace the first parameter which would break the geocoder.
Is there a way I can use closures, or pass the value of i into the anonymous function at all?
Effectively what I want to do is:
geocoder.geocode({'address': address}, function(results, status, i) {
alert(i); // 0, 1, 2, 3, 4
}(i));
but working :-)
I want to add some extra parameters to the Google geocoder API call as I'm running it in a loop, but am not sure how to append closure parameters to their anonymous function that already has default parameters that are passed in by the call to the API.
For example:
for(var i = 0; i < 5; i++) {
geocoder.geocode({'address': address}, function(results, status) {
// Geocoder stuff here
});
}
I want to be able to use the value of i in the passed geocoder.geocode() anonymous function, but if I had a closure using }(i));
on line 4 for example that would replace the first parameter which would break the geocoder.
Is there a way I can use closures, or pass the value of i into the anonymous function at all?
Effectively what I want to do is:
geocoder.geocode({'address': address}, function(results, status, i) {
alert(i); // 0, 1, 2, 3, 4
}(i));
but working :-)
Share Improve this question edited Dec 11, 2013 at 0:46 jmac 7,1282 gold badges31 silver badges58 bronze badges asked Oct 20, 2010 at 14:15 WheresWardyWheresWardy 4557 silver badges23 bronze badges 02 Answers
Reset to default 12You can access i
directly from you anonymous function (via closure), but you need to capture it so that each call to geocode
gets its own copy. As usual in javascript, adding another function will do the trick. I renamed the outer i
variable to make it clearer:
for(var iter = 0; iter < 5; iter++) {
(function(i) {
geocoder.geocode({'address': address}, function(results, status) {
// Geocoder stuff here
// you can freely access i here
});
})(iter);
}
function geoOuter(i) {
geocoder.geocode({'address': address}, function(results, status) {
// Geocoder stuff here
// This has access to i in the outer function, which will be bound to
// a different value of i for each iteration of the loop
});
}
for(var i = 0; i < 5; i++) {
geoOuter(i);
}
Oughta do it...
版权声明:本文标题:Attaching parameters with javascript closures to default parameters in anonymous functions - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740319760a2261016.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论