简体   繁体   English

将局部变量作为全局变量传递

[英]Passing local variable as a global

I am drawing a line using Google Maps V3, and I want to be able to pass a local variable loc as global, so I can use it outside of this entire code. 我正在使用Google Maps V3绘制一条线,并且希望能够将局部变量loc传递为全局变量,因此可以在整个代码之外使用它。 How can I do this? 我怎样才能做到这一点?

if(this.flightdetails.route_details.length > 0)
{
    $.each(this.flightdetails.posreports, function(i, nav)
    {
        var loc = new google.maps.LatLng(nav.latitude, nav.longitude);
        // Rest of code

        path[path.length] = loc;
        focus_bounds.extend(loc);
    });
}
var loc;
if(this.flightdetails.route_details.length > 0)
{
    $.each(this.flightdetails.posreports, function(i, nav)
    {
        loc = new google.maps.LatLng(nav.latitude, nav.longitude);
        // Rest of code

        path[path.length] = loc;
        focus_bounds.extend(loc);
    });
}
console.log(loc);

Define your variable global: 全局定义变量:

var loc; // global
if(this.flightdetails.route_details.length > 0)
{
    $.each(this.flightdetails.posreports, function(i, nav)
    {
        loc = new google.maps.LatLng(nav.latitude, nav.longitude); // with no var
        // Rest of code

        path[path.length] = loc;
        focus_bounds.extend(loc);
    });
}
if(this.flightdetails.route_details.length > 0)
{
    $.each(this.flightdetails.posreports, function(i, nav)
    {
        this.loc = new google.maps.LatLng(nav.latitude, nav.longitude);
        // Rest of code

        path[path.length] = loc;
        focus_bounds.extend(loc);
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM