我有一个javascript问题,我认为这与循环中的闭包有关,类似于:
How to copy a variable in JavaScript?
JavaScript closure inside loops – simple practical example
我正在Bing地图上绘制一个引脚列表,并在每个引脚上附加一个事件,以便为每个引脚显示一个信息框。每个引脚应该有一个不同的信息框。问题是,当事件触发时,它总是显示循环的最后一次迭代中的最后一个信息框,而不是引发该事件的引脚的信息框。这是循环结构的方法和我创建闭包的尝试失败
monumentMap.plotData = function() {
monumentMap.theMap.entities.clear();
var monument = null
var loc = null;
for (var i = monumentMap.theData.length - 1; i >= 0; i--) {
monument = monumentMap.theData[i];
loc = new Microsoft.Maps.Location(monument.lat, monument.lon);
// construct a bing pushpin and add it to the item data
monument.pin = new Microsoft.Maps.Pushpin(loc, {
icon: 'http://media.maps101.com/' + monument.pinImg
});
// construct a bing infobox and add it to the item data
monument.infobox = new Microsoft.Maps.Infobox(loc, { // breakpoint 1
title: monument.title,
offset: new Microsoft.Maps.Point(-3, monument.pin.getHeight() - 5),
zIndex: 999,
visible: true
});
// add event listeners
// doesn't work
Microsoft.Maps.Events.addHandler(monument.pin, 'mouseover', function() {
return function(infobox) {
monumentMap.displayInfobox(infobox);
}(monument.infobox); // breakpoint 2: by the time this is executed monument.infobox is already "last not current"
});
// add the entities to the map view for this item
monumentMap.theMap.entities.push(monument.pin);
}
}
我无法理解为什么它不起作用。我在循环中设置了monument.infobox,如果我在标记为“断点1”的行上放置一个断点,我可以看到每个循环迭代都会改变monument.infobox,但是当我到达“断点2”时(这不是' t执行直到事件触发)它引用我创建的最后一个信息框,而不是我在附加事件处理程序时附加到该引脚的信息框。
到目前为止,我期待行 monumentMap.theMap.entities.push(monument.pin); 在循环中添加最后一个引脚的一堆副本,但它没有吨。它可以正常工作,并在每次循环迭代时添加不同的引脚。
答案 0 :(得分:1)
您需要将参数放在外部不祥函数中。如果我是对的:
Microsoft.Maps.Events.addHandler(monument.pin, 'mouseover',
(function(infobox) {
return function() {
monumentMap.displayInfobox(infobox);
}
})(monument.infobox));