Javascript:关于在函数

时间:2015-10-30 20:13:22

标签: javascript arrays

我的代码有两个函数将数组传递给另一个函数。我第一次执行此操作时,第二次没有执行此操作。

第一次(更大功能的片段):

for (m = 0; m < totalAnimals; m++) {
    ids = mergedIDs[m];
    file = "data/" + ids + ".geojson";
    coords = arrayArray[3 * m];
    time = arrayArray[3 * m + 1];
    props = arrayArray[3 * m + 2];

    $.getJSON(file).done(function (data) { 
        $(data).each(function () {
            coords.push(data.geometry.coordinates); 
            time.push(data.properties.time);
            id = data.properties.id;
            species = data.properties.species;
            sex = data.properties.sex;
            props.push(id, species, sex);
        });
    }).done(function () {
        makeTrack(coords, time, props);
        coords = []; // clearing in between rounds
        time = [];
        props = [];
    }).fail(function () {
        console.log(ids + "multipointCoords not populated");
    });

}

上面的代码成功地将三个数组(coords,time,props)传递给makeTrack函数:

    function makeTrack(multipointCoords, tracksTime, properties) {

        // parse properties array to assign to features
        var id = properties[0],
            species = properties[1],
            sex = properties[2];

        // setup OpenLayers structure and style assignment
        var trackSource = new ol.source.Vector(); // create an empty source instance

        var lineString = new ol.geom.LineString([ // create a linestring geometry from the GeoJSON data for the tracks
            ol.proj.fromLonLat(multipointCoords[0][0])
        ]);

        var icon = new ol.geom.Point( // create a point geometry from the GeoJSON data for the header icons
            ol.proj.fromLonLat(multipointCoords[0][0])
        );

等。到目前为止一切都很棒。稍后在该函数中我调用

       segmentConstruction(multipointCoords);

然后

      function segmentConstruction(multipointCoords) {
            length = multipointCoords[0].length;

            if (i < length) {
                var timer;

                prevCoord = ol.proj.fromLonLat(multipointCoords[0][i - 1]);
                currCoord = ol.proj.fromLonLat(multipointCoords[0][i]);
                ...

我收到错误:

TypeError: multipointCoords[0] is undefined

我确实读过这个Javascript passing array as parameter to function,但我不明白传递引用和传递值之间的区别。

根据Passing an array as parameter in JavaScript,可以传递数组。

编辑数组是二维的,因为它们是[lon,lat]的数组。

0 个答案:

没有答案