是否有可能通过调用一个函数我可以获得内部调用的函数的返回值?
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
关于JavaScript : access return of other function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23504185/