从函数内部的函数返回值 [英] Returning value from function inside a function
问题描述
我正在使用 goMap
而我正在尝试在其中添加一个函数,但是在调用函数时我无法返回它。如果我在函数内部使用 alert()
,它将具有我应该返回的值。
getAddress:function(latlngcoords)
{
var goMap = this;
var input = latlngcoords;
var latlngStr = input.split(,,2);
var lat = parseFloat(latlngStr [0]);
var lng = parseFloat(latlngStr [1]);
var latlng = new google.maps.LatLng(lat,lng);
var地址;
geocoder.geocode({'latLng':latlng},function(results,status)
{
if(status == google.maps.GeocoderStatus.OK)
{
if(results)
{
address = results;
// alert(address);< - works但
}
}
});
返回地址; //根本不会回来?
},
通过执行以下操作调用: $。goMap。 getAddress()
但在参数中有纬度和经度。我需要它通过返回地址
返回值,但它根本不会返回任何内容。
我能够让它返回值吗?
geocode
是一个异步函数。当你打电话时启动,但就是这样。 (这就是它接受回调的原因。)所以你的 getAddress
函数在回调设置地址
之前返回。 / p>
您需要让 getAddress
函数接受回调,然后以这种方式返回结果,例如
getAddress:function(latlngcoords,callback)
// ^ ---回调参数
{
var goMap = this;
var input = latlngcoords;
var latlngStr = input.split(,,2);
var lat = parseFloat(latlngStr [0]);
var lng = parseFloat(latlngStr [1]);
var latlng = new google.maps.LatLng(lat,lng);
var地址;
geocoder.geocode({'latLng':latlng},function(results,status)
{
if(status == google.maps.GeocoderStatus.OK)
{
if(results)
{
address = results;
callback(address);
// ^ ---用结果$ b调用它$ b}
}
});
},
当然这意味着代码调用 getAddress
必须处理 getAddress
也是异步的事实。
I'm using goMap
and I'm trying to add a function inside it, but I cannot get it to return when the function is called. If I use alert()
inside the function, it has the values I need it that should be returned.
getAddress: function(latlngcoords)
{
var goMap = this;
var input = latlngcoords;
var latlngStr = input.split(",", 2);
var lat = parseFloat(latlngStr[0]);
var lng = parseFloat(latlngStr[1]);
var latlng = new google.maps.LatLng(lat, lng);
var address;
geocoder.geocode({'latLng': latlng}, function(results, status)
{
if(status == google.maps.GeocoderStatus.OK)
{
if(results)
{
address = results;
//alert(address); <-- works but
}
}
});
return address; // won't return at all?
},
It's called by doing: $.goMap.getAddress()
but with a latitude and longitude in the argument. I need it to return the values by return address
but it won't return anything at all.
How will I be able to get it to return the value?
geocode
is an asynchronous function. It starts when you call it, but that's all. (That's why it accepts a callback.) So your getAddress
function is returning before address
is set by the callback.
You need to have your getAddress
function accept a callback as well, and return the result that way, e.g.
getAddress: function(latlngcoords, callback)
// ^--- callback parameter
{
var goMap = this;
var input = latlngcoords;
var latlngStr = input.split(",", 2);
var lat = parseFloat(latlngStr[0]);
var lng = parseFloat(latlngStr[1]);
var latlng = new google.maps.LatLng(lat, lng);
var address;
geocoder.geocode({'latLng': latlng}, function(results, status)
{
if(status == google.maps.GeocoderStatus.OK)
{
if(results)
{
address = results;
callback(address);
// ^--- call it with the result
}
}
});
},
Naturally this means that the code calling getAddress
has to handle the fact that getAddress
is also asynchronous.
这篇关于从函数内部的函数返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!