2

我有几个函数可以评估一些变量并得出结果

例如:

function first(){
   //variable declarations
   var result1 = a+b
}

function two(){
   //variable declarations
   var result2 =c+d 
}

我想将这两个结果都传递给另一个函数

function three( result1, result2 ) {
   var finalResult = result1 + result2;
}

我的问题是我从哪里调用函数 3。因为实际上我有大约 10 个函数需要通过结果。我是否在每个末尾都放了三个(结果#)???

谢谢你

4

5 回答 5

1
function first() {
    return a + b;
}

function two() {
    return c + d;
}

function three(result1, result2) {
    return result1 + result2;
}

Call it:

var finalResult = three(first(), two());
于 2012-12-20T01:59:51.130 回答
1

A better way is to pass an object. It will make sure if you want to put more details, you wont have to add more arguments in the function and make it more readable.

function first(){
    return "value of one";
} 

function second(){
    return "value of two";
}


function three(data){
    var finalResult = data.first + data.second;
}

ANd call it like:

var data = {};
data.first = first();
data.second = second();

three(data);
于 2012-12-20T02:00:38.070 回答
1

Here is one approach you could take, although the question is a little vague.

function first(){
 return a+b;
} 

function two(){
 return c+d;
}

function three(){
 var finalResult = first() + two();
}

Or, if you didn't want the value in a function, you could do this:

<script>
 var results = first() + two();
</script>
于 2012-12-20T02:00:50.677 回答
1

不要打扰全局结果,即result1result2。只需返回数据并在准备就绪时调用 3

function first(){
 return a+b;
} 

function two(){
  return c+d;
}

function three(){
     return (first() + two());
 }
于 2012-12-20T02:02:04.010 回答
0
function one( a, b ) {
   var result1 = a + b;
   return result1;
}

function two( c, d ) {
   var result2 = c + d;
   return result2;
}

function three( a, b ) {
   var finalResult = a + b;
   return finalResult;
}

调用它使用three( one(), two() );

于 2012-12-20T02:01:50.507 回答