对象原型包含在数组中

时间:2011-09-23 21:59:47

标签: javascript google-maps-api-3

我正在研究一种从MVCArray标记中拉出lat / lng的函数。功能如下。但是,mapData数组中的每个对象都包含本机对象的原型以及lat / lng。我玩过hasOwnProperty,但没有太多运气。我在这里做了一些明显不对的事吗?

    function prepareMarkers() {
    var mapData = [];

    // All we need from our markers list is the coordinates of the marker and title if it has one
    markers.forEach(function(elem, index) {
        mapData.push({
            lat: elem.getPosition().lat(),
            lng: elem.getPosition().lng()
        });
    });

    return mapData;
}

1 个答案:

答案 0 :(得分:0)

您正在将本机对象放入mapData数组中。这就是这个。这是一个对象。

    {
        lat: elem.getPosition().lat(),
        lng: elem.getPosition().lng()
    }

所以,它的工作正是应有的。由于它们是对象,因此它们应该具有本机对象原型的方法。你在这里试图解决一个特殊的问题吗?你到目前为止所描述的一切听起来都是正确的。

在您的数组中,您可以访问以下内容:

var latitude = mapData[0].lat;
var longitude = mapData[0].lng;