Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return value from nested function in Javascript [duplicate]

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;     });       } 
like image 336
Chris Armstrong Avatar asked Apr 10 '10 02:04

Chris Armstrong


People also ask

How do you return a value from a function within a function?

To return a value from a function, you must include a return statement, followed by the value to be returned, before the function's end statement. If you do not include a return statement or if you do not specify a value after the keyword return, the value returned by the function is unpredictable.

What does || return in JavaScript?

It is typically used with boolean (logical) values. When it is, it returns a Boolean value. However, the || operator actually returns the value of one of the specified operands, so if this operator is used with non-Boolean values, it will return a non-Boolean value.

Is it possible to nest functions in JavaScript?

JavaScript allows for the nesting of functions and grants the inner function full access to all the variables and functions defined inside the outer function (and all other variables and functions that the outer function has access to).


1 Answers

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    } 
like image 173
z33m Avatar answered Sep 27 '22 22:09

z33m