通过调用一个函数,是否有可能获得内部调用的函数的返回?
function f1(...){
f2(...);
}
function f2(...){
return 123;
}
换句话说,通过仅调用
f1()
,我可以从f2返回123吗?我希望这是有道理的。
提前致谢。
编辑
我可能没有在这里做出最好的类比,所以这是我的代码:
getLocation(45.123,12.123);
function getLocation(a,b) {
document.write(lo);
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(p){ajmo(p,a,b);});
}
}
function ajmo(position,a,b) {
lat = position.coords.latitude;
lng = position.coords.longitude;
alert('kurac:' + getDistanceFromLatLonInKm(a,b,lat, lng));
}
function getDistanceFromLatLonInKm(lat_origin, lon_origin, lat_pos, lon_pos) {
var R = 6371;
var dLat = deg2rad(lat_pos - lat_origin);
var dLon = deg2rad(lon_pos - lon_origin);
var a =
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(deg2rad(lat_origin)) * Math.cos(deg2rad(lat_pos)) *
Math.sin(dLon / 2) * Math.sin(dLon / 2)
;
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = R * c;
return d;
}
function deg2rad(deg) {
return deg * (Math.PI/180)
}
我只想通过调用get.Location从函数getDistanceFromLatLonInKm的返回中访问d。在这种情况下可以吗?
最佳答案
您要返回返回值
function f1(...){
return f2(...);
}
function f2(...){
return 123;
}
alert(f1());//123