无法返回/访问函数中的变量 [英] Can't return / get access to a variable inside a function

查看:222
本文介绍了无法返回/访问函数中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下几行代码。除了设置变量UserLocation并将其打印到控制台之外,一切都可以工作。我的目标是将我在showPosition()函数中找到的坐标作为一个可以在任何地方访问的全局变量,因为我将在很多用途中使用它们。



我在这里错过了什么?我已经尝试将showPosition中的坐标变量设置为全局变量,尝试返回变量等。我现在可以将这些坐标从该函数中取出的唯一方法是将其作为参数传递给另一个函数,我不想这样做。

  if(navigator.geolocation){
var UserLocation = navigator.geolocation。 getCurrentPosition(showPosition);
console.log(UserLocation); //这不起作用,并给我undefined
} else {
alert(这个浏览器不支持地理定位);


函数showPosition(position){
var coordinates = new Array();
coordinates.push(Math.floor(position.coords.latitude));
coordinates.push(Math.floor(position.coords.longitude));
console.log(坐标)//这工程
返回坐标;


解决方案

navigator.geolocation.getCurrentPosition 是异步的。如注释中所示,这意味着在 getCurrentPosition 完成之前, UserLocation 将是未定义的。



Promise非常适合处理异步代码。以下使用ECMA6承诺设置您的全局变量:

  function getUserLocation(){
return new Promise(function解析,拒绝){
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(resolve);
} else {
reject(此浏览器不支持地理定位)
}
});


函数showPosition(position){
var coordinates = new Array();
coordinates.push(Math.floor(position.coords.latitude));
coordinates.push(Math.floor(position.coords.longitude));
console.log(坐标)//这工程
返回坐标;
}

函数setGlobalCoordsAndAlert(坐标){
window.coordinates = coordinates;
警报(坐标);

$ b getUserLocation()
.then(showPosition)
.then(setGlobalCoordsAndAlert)
.catch(function(err){
console.log(err);
});

ECMA6承诺在旧版浏览器中不可用,所以如果您担心兼容性。


I have the following lines of code. Everything works except for setting the variable "UserLocation" and printing it to the console. My goal is to get the coordinates I find in the showPosition() function as a global variable that can be accessed anywhere, because I will be using them a lot and for many purposes.

What am I missing here? I've tried setting the coordinates variable inside showPosition to a global variable, tried returning the variable, etc. The only way I can get those coordinates out of that function as of now is to pass it as a parameter to a whole other function which I don't want to do.

  if (navigator.geolocation) {
    var UserLocation = navigator.geolocation.getCurrentPosition(showPosition);
    console.log(UserLocation); // This doesn't work and gives me "undefined"
  } else {
    alert("Geolocation is not supported by this browser");
  }

  function showPosition(position) {
    var coordinates = new Array();
    coordinates.push(Math.floor(position.coords.latitude));
    coordinates.push(Math.floor(position.coords.longitude));
    console.log(coordinates) // This works
    return coordinates;
  }

解决方案

navigator.geolocation.getCurrentPosition is asynchronous. As indicated in the comments, this means that UserLocation will be undefined until getCurrentPosition has completed.

Promises are excellent for dealing with asynchronous code. The following uses ECMA6 promises to set your global variable:

function getUserLocation() {
  return new Promise(function(resolve, reject) {
      if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(resolve);
      } else {
        reject("Geolocation is not supported by this browser")
      }
  });
}

function showPosition(position) {
    var coordinates = new Array();
    coordinates.push(Math.floor(position.coords.latitude));
    coordinates.push(Math.floor(position.coords.longitude));
    console.log(coordinates) // This works
    return coordinates;
}

function setGlobalCoordsAndAlert(coordinates) {
    window.coordinates = coordinates;
    alert(coordinates);
}

getUserLocation()
  .then(showPosition)
  .then(setGlobalCoordsAndAlert)
  .catch(function(err) {
    console.log(err);
  });

ECMA6 promises aren't available in older browsers, so check out a polyfill if you're worried about compatibility.

这篇关于无法返回/访问函数中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆