谜题:闭包中的值发生了变化
我使用 http://tile.cloudmade.com/wml/latest/web-maps-lite.js 进行地理编码。
有一个包含大约 20 个地址的地址数组
addresses[n] = {where:where,who:who,contact:contact,note:note,type:type};
然后我循环该数组进行地理编码
for (var i = 0; i < addresses.length; i++) {
geocoder2.getLocations(addresses[i].where, function(response) { //a callback
return function(k){
Lat[k] = response.features[0].centroid.coordinates[0];
Lng[k] = response.features[0].centroid.coordinates[1];
latlng = new google.maps.LatLng(Lat[k], Lng[k]);
MarkerArray[k] = new google.maps.Marker({
map: map,
position: latlng,
zIndex: k,
title: addresses[k].who,
icon: icons(addresses[k].type.trim())
});}(i) // a closure function called
});
}
但它始终适用于最终索引。为什么??
I use http://tile.cloudmade.com/wml/latest/web-maps-lite.js
to geocode.
There is a address array containing around 20 addresess
addresses[n] = {where:where,who:who,contact:contact,note:note,type:type};
Then I loop the array to geocode
for (var i = 0; i < addresses.length; i++) {
geocoder2.getLocations(addresses[i].where, function(response) { //a callback
return function(k){
Lat[k] = response.features[0].centroid.coordinates[0];
Lng[k] = response.features[0].centroid.coordinates[1];
latlng = new google.maps.LatLng(Lat[k], Lng[k]);
MarkerArray[k] = new google.maps.Marker({
map: map,
position: latlng,
zIndex: k,
title: addresses[k].who,
icon: icons(addresses[k].type.trim())
});}(i) // a closure function called
});
}
But the it always works on the final index. Why??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您遇到了闭环问题。您似乎试图通过添加
return function(k)...
闭包来修复它,但这全部发生在回调函数内部,因此直到循环退出并且 < code>i 指向其最终值。您必须将该包装器推出一个级别,以便它直接位于循环内部:
或者使用 Function#bind 以避免嵌套函数。
You have the Closure Loop Problem. You appear to be trying to fix it by adding the
return function(k)...
closure, but that's all occurring inside the callback function, so it won't execute until the loop has exited andi
is pointing at its final value.You would have to push that wrapper out a level so it's directly inside the loop:
Or use Function#bind to avoid the nested function.