【问题标题】:Avoid making functions in a loop with Google Maps addEventListener避免使用 Google Maps addEventListener 在循环中创建函数
【发布时间】:2014-02-20 11:26:46
【问题描述】:

所以我有这段代码,它遍历window.towns 中的每个城镇,这是一个按城镇名称的第一个字符排序的嵌套对象。像这样:

//sample data:
window.towns = { 
    'A' : { 
        "Aberdeen" : { 
            "town_info": {
                 "google_maps": {
                     "lat" : 47.561236,
                     "lng" : 0.1235467 }
                         }
                     } , 
        "Agency Village" : {
            "town_info": {
                 "google_maps": {
                     "lat" : 47.561236,
                     "lng" : 0.1235467 }
                         }
                     } 
           },
    'B' : { //towns beginning with B
          },
    'C' : {},
 // ... all the way thru 'Z'
}

然后我的代码循环遍历字母表中的每个字母,并且对于每个字母,循环遍历以该字母开头的城镇,在谷歌地图上为每个城镇创建一个标记,并在您单击时出现一个信息窗口标记。

for (var alphabet in window.towns) {
    for (var town in window.towns[alphabet]) {

        var info = window.towns[alphabet][town].town_info;

        if (info !== undefined &&
            typeof info !== undefined &&
            info.google_maps !== undefined &&
            typeof info.google_maps !== undefined) {

            var lat = info.google_maps.lat,
                lng = info.google_maps.lng;

            info.marker =
                new window.google.maps.Marker({
                    position: new window.google.maps.LatLng(lat,lng),
                    map: map,
                    title: window.towns[alphabet][town].post_title,
                    icon: icon_image
                });

            info.marker.iwindow =
                new window.google.maps.InfoWindow({
                    content: '<strong><a href="#'+
                        window.towns[alphabet][town].post_title
                            .replace(' ','-','g').toLowerCase()+
                        '" class="town">'+
                        window.towns[alphabet][town].post_title+
                        '</a></strong>'
                });

            window.google.maps.event.addListener(info.marker, 'click', function() {
                this.iwindow.open(map,this);
            });
        }
    }
}

代码可以正常工作。但是,我收到一个 JSLint 错误,“不要在循环中创建函数”,这是有道理的。当匿名函数取决于this 的值等于当前info.marker 时,如何转换我的代码以避免在循环中创建函数?

【问题讨论】:

    标签: javascript google-maps


    【解决方案1】:

    您可以在循环之前定义一个小函数,然后在循环内引用该本地函数:

    循环之前:

    function handleMarkerClick() {
        this.iwindow.open(map,this);
    }
    

    然后,在循环中:

    for (var alphabet in window.towns) {
        for (var town in window.towns[alphabet]) {
    
        // ....
    
        window.google.maps.event.addListener(info.marker, 'click', handleMarkerClick);
    

    【讨论】:

    • 这行得通...但现在我得到“可能严格违反”,因为函数中有this。值得做些什么吗?是的:)
    • @CloverLeaf - 没有实际违规,因为addListener() 在调用时适当地设置了this。不知道为什么 jsLint 会反对 - 我想它只是不确定你知道你在做什么,即使你实际上做了。 This question about jsHint 包含一个关于如何通过特殊注释解决警告的答案 - 不确定相同的方法是否适用于 jsLint。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-24
    • 2021-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多