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; }); }
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.
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.
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).
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 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With