首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Javascript中嵌套函数的返回值

Javascript中嵌套函数的返回值
EN

Stack Overflow用户
提问于 2010-04-10 02:15:30
回答 3查看 126.5K关注 0票数 66

我有一个函数,设置如下

代码语言:javascript
运行
复制
function mainFunction() {
      function subFunction() {
            var str = "foo";
            return str;
      }
}

var test = mainFunction();
alert(test);

按照我的逻辑,这个警报应该返回'foo',但是它返回的是未定义的。我做错了什么?

更新:这是我的实际代码(这是一个使用Google进行逆向地理编码的函数)

代码语言:javascript
运行
复制
function reverseGeocode(latitude,longitude){
    var address = "";
    var country = "";
    var countrycode = "";
    var locality = "";

    var geocoder = new GClientGeocoder();
    var latlng = new GLatLng(latitude, longitude);

     return geocoder.getLocations(latlng, function(addresses) {
     address = addresses.Placemark[0].address;
     country = addresses.Placemark[0].AddressDetails.Country.CountryName;
     countrycode = addresses.Placemark[0].AddressDetails.Country.CountryNameCode;
     locality = addresses.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName;
     return country;
    });   
   }
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-04-10 02:17:47

必须调用一个函数才能返回任何内容。

代码语言:javascript
运行
复制
function mainFunction() {
      function subFunction() {
            var str = "foo";
            return str;
      }
      return subFunction();
}

var test = mainFunction();
alert(test);

或者:

代码语言:javascript
运行
复制
function mainFunction() {
      function subFunction() {
            var str = "foo";
            return str;
      }
      return subFunction;
}

var test = mainFunction();
alert( test() );

你的实际密码。返回应该是外部的,在主要功能上。回调在getLocations方法中的某个地方调用,因此它的返回值不会在主函数中接收。

代码语言:javascript
运行
复制
function reverseGeocode(latitude,longitude){
    var address = "";
    var country = "";
    var countrycode = "";
    var locality = "";

    var geocoder = new GClientGeocoder();
    var latlng = new GLatLng(latitude, longitude);

    geocoder.getLocations(latlng, function(addresses) {
     address = addresses.Placemark[0].address;
     country = addresses.Placemark[0].AddressDetails.Country.CountryName;
     countrycode = addresses.Placemark[0].AddressDetails.Country.CountryNameCode;
     locality = addresses.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName;
    });   
    return country
   }
票数 67
EN

Stack Overflow用户

发布于 2010-04-10 03:10:24

正确的。在数据可用之前,传递给getLocations()的函数不会被调用,因此在设置"country“之前返回”country“不会对您有所帮助。

您需要这样做的方式是让传递给geocoder.getLocations()的函数实际上对返回的值做任何您想做的事情。

就像这样:

代码语言:javascript
运行
复制
function reverseGeocode(latitude,longitude){
  var geocoder = new GClientGeocoder();
  var latlng = new GLatLng(latitude, longitude);

  geocoder.getLocations(latlng, function(addresses) {
    var address = addresses.Placemark[0].address;
    var country = addresses.Placemark[0].AddressDetails.Country.CountryName;
    var countrycode = addresses.Placemark[0].AddressDetails.Country.CountryNameCode;
    var locality = addresses.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName;
    do_something_with_address(address, country, countrycode, locality);
  });   
}

function do_something_with_address(address, country, countrycode, locality) {
  if (country==="USA") {
     alert("USA A-OK!"); // or whatever
  }
}

如果您可能想在每次获得位置时执行一些不同的操作,那么将函数作为附加参数传递给reverseGeocode:

代码语言:javascript
运行
复制
function reverseGeocode(latitude,longitude, callback){
  // Function contents the same as above, then
  callback(address, country, countrycode, locality);
}
reverseGeocode(latitude, longitude, do_something_with_address);

如果这看起来有点混乱,那么您可以看看Dojo中的延迟特性,它使函数之间的链接更加清晰。

票数 3
EN

Stack Overflow用户

发布于 2014-10-26 03:49:38

只是FYI,Geo编码器是异步的,所以接受的答案虽然逻辑在这个实例中并不真正有效。我更希望有一个外部对象充当您的更新者。

代码语言:javascript
运行
复制
var updater = {};

function geoCodeCity(goocoord) { 
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({
        'latLng': goocoord
    }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            updater.currentLocation = results[1].formatted_address;
        } else {
            if (status == "ERROR") { 
                    console.log(status);
                }
        }
    });
};
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2611980

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档