返回Javascript中嵌套函数的值

返回Javascript中嵌套函数的值

本文介绍了返回Javascript中嵌套函数的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  function mainFunction(){
function subFunction() {
var str =foo;
return str;
}
}

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

在我的逻辑中,该警报应该返回'foo',但是它会返回undefined。我做错了什么?



更新:以下是我的实际代码(这是使用Google API进行反向地理编码的功能)

 函数reverseGeography(纬度,经度){
var address =;
var country =;
var countrycode =;
var locality =;

var geocoder = new GClientGeocoder();
var latlng =新GLatLng(纬度,经度);

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;
返回国;
});


解决方案

你必须调用一个函数之前它可以返回任何东西。

  function mainFunction(){
function subFunction(){
var str =foo;
return str;
}
return subFunction();
}

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

或者:

  function mainFunction(){
function subFunction(){
var str =foo;
return str;
}
返回子函数;
}

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

代表您的实际代码。回报应该在主要功能之外。回调在 getLocations 方法内的某个地方被调用,因此它的返回值没有被接收到你的主函数中。

 函数reverseGeography(纬度,经度){
var address =;
var country =;
var countrycode =;
var locality =;

var geocoder = new GClientGeocoder();
var latlng =新GLatLng(纬度,经度);

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;
});
返回国家
}


I have a function that is set up as follows

function mainFunction() {
      function subFunction() {
            var str = "foo";
            return str;
      }
}

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

To my logic, that alert should return 'foo', but instead it returns undefined. What am I doing wrong?

UPDATE: Here's my actual code (it's a function for reverse-geocoding with the Google API)

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;
    });
   }
解决方案

you have to call a function before it can return anything.

function mainFunction() {
      function subFunction() {
            var str = "foo";
            return str;
      }
      return subFunction();
}

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

Or:

function mainFunction() {
      function subFunction() {
            var str = "foo";
            return str;
      }
      return subFunction;
}

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

for your actual code. The return should be outside, in the main function. The callback is called somewhere inside the getLocations method and hence its return value is not recieved inside your main function.

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
   }

这篇关于返回Javascript中嵌套函数的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 04:04