Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Console.log(';变量';)-如何显示_Javascript_Jquery_Function_Google Maps_Google Maps Api 3 - Fatal编程技术网

Javascript Console.log(';变量';)-如何显示

Javascript Console.log(';变量';)-如何显示,javascript,jquery,function,google-maps,google-maps-api-3,Javascript,Jquery,Function,Google Maps,Google Maps Api 3,这里我有一个函数route() 然后我进入控制台: ReferenceError: startLocation is not defined get stack: function () { [native code] } message: "startLocation is not defined" set stack: function () { [native code] } __proto__: Error 如何在全局变量中显示此起始和结束纬度、经度数据,以便在其他函数中使用 更新:我

这里我有一个函数
route()

然后我进入控制台:

ReferenceError: startLocation is not defined
get stack: function () { [native code] }
message: "startLocation is not defined"
set stack: function () { [native code] }
__proto__: Error
如何在全局变量中显示此起始和结束纬度、经度数据,以便在其他函数中使用

更新:我真正需要的是使用函数ROUTE()获取已创建标记的位置。方法:


startLocation是此函数的局部变量。您需要从这里返回它以从其他函数访问。或者,使用类范围。为此,请阅读任何有关javascript的标准书籍。您也可以将其设置为全局变量,但我不推荐。

在任何编程语言(包括javascript)中使用全局变量通常都不是一个好主意。理想情况下,应该声明要在函数内部和外部作用域上的其他函数中访问的变量

例如,假设您的代码是这样的

var valueOne;
function setValue(){
    valueOne = 3;
}
function readValue(){
    console.log(valueOne);
}
setValue();
readValue();
仅当在readValue之前调用setValue时,才会记录“3”。在这种情况下,您必须100%确保变量是在包含这两个函数的范围内声明的,并且它们是按该顺序调用的


另一种方法是修改自己的代码,使用window.startLocation、window.endLocation等,将变量附加到窗口对象,使其全局可用。默认情况下,在javascript中使用不带前缀的变量
var
var
)会使其成为全局变量,但如果不起作用,则可以尝试将其直接附加到窗口对象。在任何情况下,您必须确保设置要在另一个函数中使用的日志值的函数在另一个函数之前被调用,如果不是的话,这些值显然不会被设置。

是的,但同样如此。您也可以使用window将变量声明为全局变量。startLocation=“myStartLocation”;当您想要显示它时:console.log(window.startLocation);请用代码解释我…@MarcoJordan,当你声明变量var时;执行window.startLocation。尝试在函数外部/之前创建对象
startLocation
endLocation
,并让它们在函数内部填充。这样,您就可以访问它们,并且可以在console.log和其他功能中访问它们。最新的问题是我真正的问题,如何获得已创建标记的位置
ReferenceError: startLocation is not defined
get stack: function () { [native code] }
message: "startLocation is not defined"
set stack: function () { [native code] }
__proto__: Error
function route() {
      // Clear any previous route boxes from the map
      clearBoxes();

      // Convert the distance to box around the route from miles to km
      distance = parseFloat(document.getElementById("distance").value) * 1.609344;

      var request = {
        origin: document.getElementById("from").value,
        destination: document.getElementById("to").value,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
      }

      // Make the directions request
      directionService.route(request, function(result, status) {
        if (status == google.maps.DirectionsStatus.OK) {
          directionsRenderer.setDirections(result);

          // Box around the overview path of the first route
          var path = result.routes[0].overview_path;
          var boxes = routeBoxer.box(path, distance);
          drawBoxes(boxes);
        } else {
          alert("Directions query failed: " + status);
        }
      });
    }
var valueOne;
function setValue(){
    valueOne = 3;
}
function readValue(){
    console.log(valueOne);
}
setValue();
readValue();