内部循环闭包 - 不确定如何解决

时间:2014-05-13 17:06:45

标签: javascript

for (var i = 0; i < stops.length; i++) {
    var code = stops[i].atcocode;
    var name = stops[i].common;
    var direction = stops[i].direction;
    var alertMessage = "View departures for " + stops[i].common + (directionText !== 'Unknown' ? (" (facing " + directionText + ")") : "") + "?";

    this.map.addMarker({
        icon: icon,
        position: new plugin.google.maps.LatLng(stops[i].latitude, stops[i].longitude)
    }, function(markerCallback) {
        markerCallback.code = code;
        markerCallback.name = name;
        markerCallback.direction = direction;
        markerCallback.alert = alertMessage;

        markerCallback.addEventListener(plugin.google.maps.event.MARKER_CLICK, function(clickedMarker) {
            alert(markerCallback.name);
        });

        _this.busStopMarkers.push(markerCallback);
    });
}

JavaScript closure inside loops – simple practical example

我已经查看了上面的问题,并且不确定如何将相同的答案逻辑应用到我的场景中。有人可以告诉我一个例子,我如何让警报显示数组中的索引项而不是最后一项?

2 个答案:

答案 0 :(得分:1)

创建一个回调工厂:

function createCallback(_this, code, name, direction, alertMessage) {
    return function(markerCallback) {
        markerCallback.code = code;
        markerCallback.name = name;
        markerCallback.direction = direction;
        markerCallback.alert = alertMessage;

        markerCallback.addEventListener(plugin.google.maps.event.MARKER_CLICK, function(clickedMarker) {
            alert(markerCallback.name);
        });

        _this.busStopMarkers.push(markerCallback);
    };
}

然后用它在循环中创建一个函数:

for (var i = 0; i < stops.length; i++) {
    var code = stops[i].atcocode;
    var name = stops[i].common;
    var direction = stops[i].direction;
    var alertMessage = "View departures for " + stops[i].common + (directionText !== 'Unknown' ? (" (facing " + directionText + ")") : "") + "?";

    var callback = createCallback(_this, code, name, direction, alertMessage);

    this.map.addMarker({
        icon: icon,
        position: new plugin.google.maps.LatLng(stops[i].latitude, stops[i].longitude)
    }, callback);
}

答案 1 :(得分:0)

您可以创建这样的匿名函数:

for (var i = 0; i < stops.length; i++) {
    (function(me, stop){ // Use the stop as an argument, this function is directly called
        var code = stop.atcocode;
        var name = stop.common;
        var direction = stop.direction;
        var alertMessage = "View departures for " + stop.common + (directionText !== 'Unknown' ? (" (facing " + directionText + ")") : "") + "?";

        me.map.addMarker({
            icon: icon,
            position: new plugin.google.maps.LatLng(stop.latitude, stop.longitude)
        }, function(markerCallback) {
            markerCallback.code = code;
            markerCallback.name = name;
            markerCallback.direction = direction;
            markerCallback.alert = alertMessage;

            markerCallback.addEventListener(plugin.google.maps.event.MARKER_CLICK, function(clickedMarker) {
                alert(markerCallback.name);
            });

            _this.busStopMarkers.push(markerCallback);
        });
    })(this, stops[i]); // Pass the stop

}