简体   繁体   English

为什么我不能访问嵌套 function 中的变量?

[英]Why can't I access variable inside a nested function?

So I have been using the geolocation to find the user location and then find the distance to it and a csv list of locations.所以我一直在使用地理位置来查找用户位置,然后找到到它的距离和 csv 位置列表。 I want to save the distance to the json object, but can't access it in my nested function.我想保存到 json object 的距离,但无法在我的嵌套 function 中访问它。

function onLocationFound(e) {
    console.log(e);
    L.marker([e.latitude, e.longitude], {icon: home_marker}).bindPopup("You are here!").addTo(m);
    console.log(typeof(e.latitude));
    console.log(typeof(e.longitude));
    $.get('vac_sites.csv', function (csvString) {

        var data = Papa.parse(csvString, { header: true, dynamicTyping: true }).data;
        console.log(data)
        for (let i = 0; i < data.length; i++) {
        //get distance and then use dynamic variable names, make them into js objects, then store in dic with distance
          var row = data[i];
          console.log(row)
          var myRoute = L.Routing.osrmv1({
            serviceUrl: 'https://xxx.xxx.xxx:443/route/v1'
          });
          var self_loc = new L.Routing.Waypoint;
          self_loc.latLng = L.latLng([e.latitude, e.longitude]);
          var site_loc = new L.Routing.Waypoint;
          site_loc.latLng = L.latLng([row.Latitude, row.Longitude]);
          myRoute.route([self_loc, site_loc], function(err, routes) {
              distance = routes[0].summary.totalDistance;
              console.log('routing distance: ' + distance);
              row.distance = distance
              console.log(row)
          });

When I open the console, it appears to have created a new json object and added row to it.当我打开控制台时,它似乎已经创建了一个新的 json object 并添加了行。 How can I access the original row variable and add the distance to it?如何访问原始行变量并添加距离? Is it a problem with function scope? function scope 有问题吗?

EDIT: When I open console I get this for the first console.log(row):编辑:当我打开控制台时,我得到了第一个 console.log(row):

...
{Name: "Cate Pharmacy", Address: "500 N Missouri Ave, Corning, Arkansas", Telephone: "(870) 857-6766", Website: "<a href="https://www.catepharmacy.com/" target="_b…ow noopener noreferrer">www.catepharmacy.com/</a>", Latitude: 36.4155144, …}
...

I want to add a key and value pair for this that is the distance of the route in the form distance: xxxxx.我想为此添加一个键值对,即路径距离,格式为距离:xxxxx。 Desired result is:期望的结果是:

...
{Name: "Cate Pharmacy", Address: "500 N Missouri Ave, Corning, Arkansas", Telephone: "(870) 857-6766", Website: "<a href="https://www.catepharmacy.com/" target="_b…ow noopener noreferrer">www.catepharmacy.com/</a>", Latitude: 36.4155144, distance: xxxxxx, …}
...

But instead at the second console.log(row) I get this:但是在第二个 console.log(row) 我得到了这个:

{Name: null, distance: 265184.8}

Talking on the chat we solved the problem.在聊天中我们解决了这个问题。

It was narrowed down to changing var row;它被缩小到改变var row; to let row ; let row ;

It sounds like row is leaked into your function somehow.听起来row以某种方式泄漏到您的 function 中。 You should read this to understand the differences between the two variable declaration keywords.您应该阅读本文以了解两个变量声明关键字之间的区别。 In a nutshell, var is bound to the immediate function body while let is bound to the immediate closing block.简而言之, var 绑定到立即 function 主体,而 let 绑定到立即关闭块。 That could be what caused it.这可能是造成它的原因。 Otherwise, I don't know.否则,我不知道。

It's best to use let because var is almost always unnecessary and can cause problems.最好使用let ,因为var几乎总是不必要的,并且会导致问题。

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

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