关于javascript:如何将参数传递给addEventListener监听器函数?

How can I pass arguments to addEventListener listener function?

本问题已经有最佳答案,请猛点这里访问。

Possible Duplicate:
Javascript closure inside loops - simple practical example
Please explain the use of JavaScript closures in loops

1
2
3
4
5
6
for (var i = 0; i < pois.length; i++) {
    pois[i].marker.addEventListener('dblclick', function (data) {
        var infoWindow = new BMap.InfoWindow(pois[i].address, opts);  
        map.openInfoWindow(infoWindow, map.getCenter());    
    })
}

这是我的代码。
显然这里有一个错误

new BMap.InfoWindow(pois[i].address, opts);

如何在pois [i] .marker监听器功能中使用pois [i] .address?


1
2
3
4
5
6
7
8
9
10
11
for (var i = 0; i < pois.length; i++) {
    setupEventListener(pois[i]);
}

function setupEventListener(obj) {
    var address = pois[i].address;
    obj.marker.addEventListener('dblclick', function (data) {
        var infoWindow = new BMap.InfoWindow(address, opts);  
        map.openInfoWindow(infoWindow, map.getCenter());    
    });
}