[removed] access return of other function

2019-07-17 21:15发布

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?

4条回答
Summer. ? 凉城
2楼-- · 2019-07-17 21:21

If you want to save the return value use this:

function f1(...){
   var f2return = f2(...);
}

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

If you want to return it directly use @Travis Js code.

I would propose to save the return value into a variable for further processing, otherwise I would just call f2 directly. Keep in mind that any return statement exits the current function.

查看更多
放我归山
3楼-- · 2019-07-17 21:23

You want to return the returned value

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

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

alert(f1());//123
查看更多
干净又极端
4楼-- · 2019-07-17 21:40

You Can also Pass the context this to the second function . However , in this case , you must call f2 as following:

function f1(){

   return f2.call(this)

}

--

function f2(){
   return 123
}

UPDATE :

Passing context is very useful when you have Oriented Object Programming (OOP):

I.E :

 function Person(bornYear){
    this.born=bornYear ; 
    this.age=Age.call(this,2014);   
     return this;
  }

function Age(currentYear){

  return currentYear-this.age; 

}

If you note , this.age is not undefined because the context this has been passed from Person function to Age function.

Test this example :

var abdennour=new Person(1989);
console.log(abdennour.age) // 25
查看更多
Rolldiameter
5楼-- · 2019-07-17 21:41

Why not?

<!DOCTYPE html>

<html>

<head>

</head>

<body>
    <script>
        alert(f1());
        function f2() {
            return "Hello, I'm here";
        };
        function f1() {
            return f2();
        };
    </script>
</body>

</html>
查看更多
登录 后发表回答