如何从另一个函数中读取变量?

时间:2015-01-19 23:35:31

标签: javascript google-maps-api-3 socket.io pug

在下面的代码中,似乎函数initialize()一旦被socket.on('results')触发就无法读取“array”的值。

block content
    script.
        function initialize() {
            var mapOptions = {
                center: { lat: -30, lng: 150},
            };
            var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
            setMarkers(map, array);
        }
        function setMarkers(map, locations) {
            for (var i = 0; i < locations.length; i++) {
                var location = locations[i];
                var myLatLng = new google.maps.LatLng(location[1], location[2]);
                var marker = new google.maps.Marker({
                    position: myLatLng,
                    map: map,
                    title: location[0]
                });
            }
        }
        google.maps.event.addDomListener(window, 'load', initialize);
    div(id="map-canvas")
    script.
        var socket = io.connect('http://localhost:4000');
        socket.on('results', function(results) {
            var array = [];
            results.forEach(function(item){
                name = item.name;
                coordinates = item.location.coordinate;
                array.push([name, coordinates.latitude, coordinates.longitude]);
            });
            initialize();
        });

2 个答案:

答案 0 :(得分:1)

在调用initialize函数时,将数组简单地作为参数传递。

initialize(array);

他们,在函数初始化中:

function initialize(array) {REST OF THE CODE}

所有代码:

block content
    script.
        function initialize(array) {
            var mapOptions = {
                center: { lat: -30, lng: 150},
            };
            var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
            array = array || [];
            setMarkers(map, array);
        }
        function setMarkers(map, locations) {
            for (var i = 0; i < locations.length; i++) {
                var location = locations[i];
                var myLatLng = new google.maps.LatLng(location[1], location[2]);
                var marker = new google.maps.Marker({
                    position: myLatLng,
                    map: map,
                    title: location[0]
                });
            }
        }
        google.maps.event.addDomListener(window, 'load', initialize);
    div(id="map-canvas")
    script.
        var socket = io.connect('http://localhost:4000');
        socket.on('results', function(results) {
            var array = [];
            results.forEach(function(item){
                name = item.name;
                coordinates = item.location.coordinate;
                array.push([name, coordinates.latitude, coordinates.longitude]);
            });
            initialize(array);
        });

答案 1 :(得分:0)

数组变量在initialize函数中不可见。它在回调匿名函数中关闭。我建议将数组作为参数传递:

block content
    script.
        function initialize(array) {
            var mapOptions = {
                center: { lat: -30, lng: 150},
            };
            var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
            array = array || [];
            setMarkers(map, array);
        }
        function setMarkers(map, locations) {
            for (var i = 0; i < locations.length; i++) {
                var location = locations[i];
                var myLatLng = new google.maps.LatLng(location[1], location[2]);
                var marker = new google.maps.Marker({
                    position: myLatLng,
                    map: map,
                    title: location[0]
                });
            }
        }
        google.maps.event.addDomListener(window, 'load', initialize);
    div(id="map-canvas")
    script.
        var socket = io.connect('http://localhost:4000');
        socket.on('results', function(results) {
            var array = [];
            results.forEach(function(item){
                name = item.name;
                coordinates = item.location.coordinate;
                array.push([name, coordinates.latitude, coordinates.longitude]);
            });
            initialize(array);
        });