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

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

问题描述

我有以下几行代码.除了设置变量"UserLocation"并将其打印到控制台外,其他所有操作均有效.我的目标是获取在showPosition()函数中找到的坐标作为可以在任何地方访问的全局变量,因为我将大量使用它们并用于许多用途.

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.

我在这里想念什么?我已经尝试将showPosition中的坐标变量设置为全局变量,尝试返回该变量,等等.到目前为止,我可以从该函数中获取那些坐标的唯一方法是将其作为参数传递给整个其他函数,我不想做.

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 是异步的.如评论中所示,这意味着在 getCurrentPosition 完成之前, UserLocation 将是未定义的.

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

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

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 Promise在较旧的浏览器中不可用,因此如果您担心兼容性,请查看一个polyfill.

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

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

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