简体   繁体   中英

How to access local variable outside a function in JavaScript?

Please help me to sort out below codes-

    people.nearby({latitude: 43.646838, longitude: -79.403723}, 5000, function(err, people){
  if(err) console.error(err)
  else console.log('people nearby:', people)
})

places.nearby({latitude: 43.646838, longitude: -79.403723}, 5000, function(err, places){
  if(err) console.error(err)
  else console.log('places nearby:', places)
})

I want to access and merge 'people' and 'places' just after 2nd brackets closing })

Yes you can use a global object to access both at a time

var mergedObject = {};
people.nearby({latitude: 43.646838, longitude: -79.403723}, 5000, function(err, people){
  if(err) console.error(err)
  else {
   mergedObject['people'] = people;
   console.log('people nearby:', people);
   }
})

places.nearby({latitude: 43.646838, longitude: -79.403723}, 5000, function(err, places){
  if(err) console.error(err)
  else {
       console.log('places nearby:', places);
       mergedObject['places'] = places;
      }
})
personGeoLocation = {};
people.nearby({latitude: 43.646838, longitude: -79.403723}, 5000, function(err, people){
  if(err) console.error(err)
  else personGeoLocation.person = people;
})

places.nearby({latitude: 43.646838, longitude: -79.403723}, 5000, function(err, places){
  if(err) console.error(err)
  else personGeoLocation.places = places;
})

You can use javascript closures in order to acomplish that. Any variable you define with var is available to the inner structures. You can define an external var and write it from inside the callbacks.

On the other hand, the real problem you may face here is if people#nearby is async, the data will not be available after the blocks you are showing, so you need something to sync both, for example an additional function:

var globalPeople = null;
var globalPlaces = null;

function checkWeHaveBoth() {
  if (globalPeople && globalPlaces)
    doSometing(globalPeople, globalPlaces)
}

people.nearby({latitude: 43.646838, longitude: -79.403723}, 5000, function(err, people){
  if(err) console.error(err)
  else {
    globalPeople = people;
    checkWeHaveBoth();
  }
})

places.nearby({latitude: 43.646838, longitude: -79.403723}, 5000, function(err, places){
  if(err) console.error(err)
  else {
    globalPlaces = places;
    checkWeHaveBoth();
  }
})

Now you can write doSomething() knowing it will be called when both results are available.

nearby passes the callback an array of objects with the positions, so you process it like any other array:

globalPeople[0].latitude

Or traverse it:

globalPeople.forEach(...)

For example to just show it:

function doSomething() {
    console.log(globalPeople);
    console.log(globalPlaces);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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