簡體   English   中英

Javascript對象文字范圍問題?

[英]Javascript Object Literal Scope Issue?

我很難理解為什么this.$mapthis.markers未定義。 這是我正在使用的代碼,並且在我希望這些變量提供值的地方添加了注釋:

(function($) {
    'use strict';

    var A = {

        /**
         * Initialize the A object
         */
        init: function() {
            this.$map = this.renderMap();
            this.markers = this.getMarkers();

            this.renderMarkers();
        },

        renderMap: function() {

            var url = 'http://a.tiles.mapbox.com/v3/delewis.map-i3eukewg.jsonp';

            // Get metadata about the map from MapBox
            wax.tilejson(url, function(tilejson) {
                var map = new L.Map('map', {zoomControl: false});

                var ma = new L.LatLng(42.2625, -71.8028);

                map.setView(ma, 8);

                // Add MapBox Streets as a base layer
                map.addLayer(new wax.leaf.connector(tilejson));

                return function() {
                    return map;
                };
            });
        },

        renderMarkers: function() {
            var geojsonLayer = new L.GeoJSON(null, {
                pointToLayer: function (latlng){
                    return new L.CircleMarker(latlng, {
                        radius: 8,
                        fillColor: "#ff7800",
                        color: "#000",
                        weight: 1,
                        opacity: 1,
                        fillOpacity: 0.8
                    });
                }
            });

            geojsonLayer.addGeoJSON(this.markers); // this.markers is undefined
            this.$map.addLayer(geojsonLayer); // this.$map is undefined

        },

        getMarkers: function() {
            $.getJSON("/geojson/", function (data) {
                return data;
            });
        }
    };

    /**
     * A interactions
     */
    $(document).ready(function() {
        A.init()
    });

})(jQuery.noConflict());

我花了整整一天的時間進行搜索,但我認為這里缺少一些基本知識,但我不明白。

renderMapgetMarkers方法都不返回任何值,因此它們的返回值是undefined

看來您正在嘗試通過ajax請求初始化這些字段,但這不一定是一個好主意。

您可能應該做的是:

getMarkers: function(callback){
    var result = this;
    $.getJSON("url", function(jsonData){ 
        result.markers = jsonData; 
        if(callback) callback()
     });
},

當它們可用時,它將懶惰地初始化對象的字段。

注意 :AJAX是異步的,您不能依靠此回調快速設置成員,或者甚至永遠(可能失敗)。 這表明您需要多考慮一些設計,並嘗試更多使用回調。

例如,按上述方法修改getMarkersrenderMap函數,以獲取存儲數據后調用的回調,然后將init更改為:

init: function(){
    var res = this;
    var post_init_callback = function(){
        if(res.$map != undefined && res.markers!=undefined){
            //this will only run after both ajax calls are complete
            res.renderMarkers();  
        }
    };
    this.getMarkers(post_init_callback);
    this.renderMap(post_init_callback);
},

這里的問題是您在另一個函數中調用了return。 您實際上所做的是將getMarkers (最簡單的示例)定義為:

getMarkers: function() {
    $.getJSON('/geojson/', some_random_func);
}

在這一點上,很令人討厭的是getMarkers實際上沒有返回任何東西(ergo undefined)。 renderMap函數也是如此。 同樣在這種情況下,您的“ some_random_func”被定義為function(data) { return data; } function(data) { return data; }但是什么呢它返回? 事實是some_random_func由jQuery本身調用,而AFAIK jQuery完全不關心成功函數的返回值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM