Javascript 试图调用数组中的对象,返回未定义的

Javascript 试图调用数组中的对象,返回未定义的,javascript,arrays,object,Javascript,Arrays,Object,我试图调用数组中唯一的对象,但它总是返回undefined。如果我记录整个数组,我可以看到其中的所有内容,如果我调用它的对象,它将返回undefined 守则: var pos = []; this code is inside a function____ if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(function(position) { pos.latlng = {

我试图调用数组中唯一的对象,但它总是返回undefined。如果我记录整个数组,我可以看到其中的所有内容,如果我调用它的对象,它将返回undefined

守则:

var pos = [];

this code is inside a function____

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) { 
        pos.latlng = {
          lat: position.coords.latitude,
          lng: position.coords.longitude
        };  
        console.log(position.coords.latitude);

    });
}

 console.log(pos.latlng);  <--- undefined
 console.log(pos);         <--- works fine
 console.log(pos[0].latlng); <--- also tried this

我建议将新对象推到
pos

pos.push({
    lat: position.coords.latitude,
    lng: position.coords.longitude
});
那么访问

pos[0].lat

我建议将新对象推到
pos

pos.push({
    lat: position.coords.latitude,
    lng: position.coords.longitude
});
那么访问

pos[0].lat

这里的问题是getCurrentPosition是异步的。作为参数提供的函数,也称为回调函数,在获取当前位置之前不会被调用


我不知道您在这里使用什么来获取当前位置,但您需要确保在调用回调之前不使用结果。

这里的问题是getCurrentPosition是异步的。作为参数提供的函数,也称为回调函数,在获取当前位置之前不会被调用


我不知道您在这里使用什么来获取当前位置,但您需要确保在调用回调之前不使用结果。

这是因为您首先将pos定义为数组,然后将其视为匿名对象。将其作为数组或匿名对象

如果您想拥有latlan属性,则在代码开头定义pos,如下所示:

pos = {};
否则,您不能拥有数组的属性。因此,您需要删除它,并将0索引作为lon,将第一个索引作为纬度:

pos = [];
pos.push(position.coords.longitude); // pos[0];
pos.push(position.coords.latitude); // pos[1];

这是因为您首先将pos定义为数组,然后将其视为匿名对象。将其作为数组或匿名对象

如果您想拥有latlan属性,则在代码开头定义pos,如下所示:

pos = {};
否则,您不能拥有数组的属性。因此,您需要删除它,并将0索引作为lon,将第一个索引作为纬度:

pos = [];
pos.push(position.coords.longitude); // pos[0];
pos.push(position.coords.latitude); // pos[1];
根据我所说的

你可以承诺重新热爱你的职位:

var pos = {};

this code is inside a function____

    getPos(){

      return new Promise(function(resolve){
        if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) { 
             resolve({
              lat: position.coords.latitude,
              lng: position.coords.longitude
            });  
        });
      });
    }

getPos().then(function(pos){

  console.log(pos.lat);
  console.log(pos.lng);
})
根据我所说的

你可以承诺重新热爱你的职位:

var pos = {};

this code is inside a function____

    getPos(){

      return new Promise(function(resolve){
        if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) { 
             resolve({
              lat: position.coords.latitude,
              lng: position.coords.longitude
            });  
        });
      });
    }

getPos().then(function(pos){

  console.log(pos.lat);
  console.log(pos.lng);
})

pos
应该是一个对象(但这不是错误的来源)

该错误可能是由于函数
getCurrentPosition
的异步性造成的。如果它异步工作,那么日志记录将在值分配给
pos
之前发生。您应该在
getCurrentPosition
的回调中进行日志记录,或者在函数中封装日志记录并从回调中调用它

var pos = {}; // object

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) { 
        pos.latlng = {
            lat: position.coords.latitude,
            lng: position.coords.longitude
        };


        // SOLUTION ONE: use pos here
        console.log(pos);
        console.log(pos.lating);

        // SOLUTION TWO: call a function that do the logging
        logPos();
    });
}

function logPos() {  // will be called after the value is assigned
    console.log(pos);
    console.log(pos.lating);
}

pos
应该是一个对象(但这不是错误的来源)

该错误可能是由于函数
getCurrentPosition
的异步性造成的。如果它异步工作,那么日志记录将在值分配给
pos
之前发生。您应该在
getCurrentPosition
的回调中进行日志记录,或者在函数中封装日志记录并从回调中调用它

var pos = {}; // object

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) { 
        pos.latlng = {
            lat: position.coords.latitude,
            lng: position.coords.longitude
        };


        // SOLUTION ONE: use pos here
        console.log(pos);
        console.log(pos.lating);

        // SOLUTION TWO: call a function that do the logging
        logPos();
    });
}

function logPos() {  // will be called after the value is assigned
    console.log(pos);
    console.log(pos.lating);
}

实际上,数组是一个对象,除了它的属性是介于0到2-2和length属性之间的数字外,因此可以执行以下操作:

 var pos = [];
    pos.inneObject= {//object
      prop: 'property'
    };
pos.innerObject将存在。但是如果您尝试
console.log(pos)
您将得到“[]”,但是如果您这样做了

for (properties in pos){
console.log(pos[properties])// you will get pos.innerObject
}
因此,你的问题是另一回事!在这里,在创建属性之前先记录属性,然后在
navigator.geolocation.getCurrentPosition
的回调中移动console.log,如下所示:

var pos = [];

this code is inside a function____

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) { 
        pos.latlng = {
          lat: position.coords.latitude,
          lng: position.coords.longitude
        };  
        console.log(position.coords.latitude);

 console.log(pos.latlng);  <--- undefined
 console.log(pos);         <--- works fine
 console.log(pos[0].latlng); <--- also tried this

    });
}
var pos=[];
此代码位于函数内部____
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(函数(位置){
位置latlng={
纬度:位置坐标纬度,
lng:position.coords.longitude
};  
控制台.日志(位置.坐标.纬度);

console.log(pos.latlng);实际上数组是一个对象,除了它的属性是介于0到2-2和length属性之间的数字之外,所以可以执行以下操作:

 var pos = [];
    pos.inneObject= {//object
      prop: 'property'
    };
pos.innerObject将存在。但是如果您尝试
console.log(pos)
您将得到“[]”,但如果您这样做了

for (properties in pos){
console.log(pos[properties])// you will get pos.innerObject
}
因此,您的问题是另一个问题!在创建属性之前,请在
navigator.geolocation.getCurrentPosition
的回调中移动console.log,如下所示:

var pos = [];

this code is inside a function____

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) { 
        pos.latlng = {
          lat: position.coords.latitude,
          lng: position.coords.longitude
        };  
        console.log(position.coords.latitude);

 console.log(pos.latlng);  <--- undefined
 console.log(pos);         <--- works fine
 console.log(pos[0].latlng); <--- also tried this

    });
}
var pos=[];
此代码位于函数内部____
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(函数(位置){
位置latlng={
纬度:位置坐标纬度,
lng:position.coords.longitude
};  
控制台.日志(位置.坐标.纬度);

console.log(pos.latlng);关于尝试访问它外部的Asynchrouns函数内的值集的老问题!Plus
pos
是一个数组而不是一个对象。您使用它的方式应该声明为:
var pos={}
。我也尝试过这个方法!还返回未定义的值。请添加
console.log(pos)的结果;编辑了这篇文章,这是一个关于尝试访问它外部的Asynchrouns函数中的值集的老问题!Plus
pos
是一个数组而不是一个对象。您使用它的方式应该声明为:
var pos={}
。我也尝试过这个!还返回未定义的值。请添加
console.log的结果(pos);编辑了这篇文章,它在那里忘了提到!但是我也尝试了这个,它返回:无法读取未定义的属性'lat',为什么不能创建像pos.latlng={lat:position.coords.latitude,lng:position.coords.longitude}这样的键值对;?您应该提到,依赖于异步函数的所有代码都应该在该函数内部或在该函数完成后专门使用!它在initmap()内部函数,但我也试过了,它返回:无法读取未定义的属性'lat',为什么不能创建像pos.latlng={lat:po]这样的键值对