Javascript:访问其他函数的返回值 [英] Javascript : access return of other function

查看:107
本文介绍了Javascript:访问其他函数的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 函数f1(..)有没有可能通过调用一个函数我可以得到内部函数的返回? 。){
f2(...);
}

函数f2(...){
return 123;

$ / code>

换句话说,只调用 f1()我可以从f2返回123吗?
我希望它是有道理的。



预先致谢。



编辑



我可能没有在这里做出最好的比喻,所以这是我的代码:

  getLocation(45.123,12.123); 

函数getLocation(a,b){
document.write(lo);
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(function(p){ajmo(p,a,b);});
}
}

函数ajmo(position,a,b){
lat = position.coords.latitude;
lng = position.coords.longitude;
alert('kurac:'+ getDistanceFromLatLonInKm(a,b,lat,lng));



函数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;
}

函数deg2rad(deg){
return deg *(Math.PI / 180)
}

我想从函数getDistanceFromLatLonInKm的返回中访问d,只需调用get.Location.Is就可以了?


<您希望返回返回的值

 函数f1( ...){
返回f2(...);
}

函数f2(...){
return 123;
}

alert(f1()); // 123


Is it possible that by calling one function I can get the return of function called inside?

function f1(...){
   f2(...);
}

function f2(...){
   return 123;
}

In other words by calling only f1() can I get 123 from f2 return? I hope it makes sense.

Thanks in advance.

EDIT

I probably didn't make the best analogy here so this is my code:

    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)
}

I want to access d from return of function getDistanceFromLatLonInKm just by calling get.Location.Is it possible in this case?

解决方案

You want to return the returned value

function f1(...){
 return f2(...);
}

function f2(...){
 return 123;
}

alert(f1());//123

这篇关于Javascript:访问其他函数的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆