函数在对它们进行操作之前接收所有变量?

时间:2017-04-24 20:45:27

标签: javascript function parameter-passing

我接收多个变量的功能有问题,然后对它们进行操作。让我们举个简单的例子:

function first(){
    var one = 1;
    result(one); // I mean it should be result(one, null); but I want to pass second variable later
}

//lets assume function first get some asyns operations...
setTimeout(function(){
    first();
}, 2000);

function second(){
    var two = 2;
    result(two); // should be result(null, two), but one will be passed after a 2 seconds and I want to sum them
}

second();

function result(one, two){
    var ultraHardMath = one + two;
}

所以我想等到result()接收所有需要的变量(一和二)之后再继续使用ultraHardMath。怎么做?

2 个答案:

答案 0 :(得分:2)

您可以将传入的参数存储在函数本身(this)中,然后在考虑所有参数后执行函数的逻辑:

function result(one, two) {
  if (one !== undefined) this.one = one;
  if (two !== undefined) this.two = two;
  if (this.one === undefined || this.two === undefined) return;
  var ultraHardMath = this.one + this.two;
}

通过省略尾随参数或将undefined传递给您不想提供的参数来实现:

result(undefined, 2);
...
result(1);

这个逻辑可以概括为一个"召回"传入参数并在考虑所有参数时调用原始函数。它是一种部分应用程序,也关心所提供的参数"在中间":

// returns a function that recalls arguments passed to function fn
// calls fn when the number of arguments recalled reaches n
function recall(fn, n) {
  var stored = []; // recalled arguments
  return function() {
    var args = [].slice.call(arguments);
    // store the passed in arguments
    for (var i = 0; i < args.length; ++i) {
      if (args[i] !== undefined) stored[i] = args[i];
    }
    // check if all arguments are accounted for
    if (stored.length < n) return;
    for (i = 0; i < n; ++i) {
      if (stored[i] === undefined) return;
    }
    // call the original function with the stored arguments
    return fn.apply(null, stored);
  };
}

现在,拥有它,你的功能可以保持不变,但只是被它的&#34;召回&#34;版本:

var result = function(one, two) {
  var ultraHardMath = one + two;
}
result = recall(result, 2);

它的工作方式与以前相同:

result(undefined, 2);
...
result(1);

recall可以简化一下:

function recall(fn, n) {
  var stored = [];
  return function() {
    var all = true;
    for (var i = 0; i < n; ++i) {
      if (arguments[i] !== undefined) stored[i] = arguments[i];
      if (stored[i] === undefined) all = false;
    }
    if (all) return fn.apply(null, stored);
  };
}

答案 1 :(得分:1)

您可以使用以下代码: 它使用闭包概念,因此结果函数可以等待。 这是你在找什么?

&#13;
&#13;
(function () {
            var funcFoo;
            function first() {
                var one = 1;
                funcFoo(one); // I mean it should be result(one, null); but I want to pass second variable later
            }
    
            //lets assume function first get some asyns operations...
            setTimeout(function () {
                first();
            }, 2000);
    
            function second() {
                var two = 2;
                funcFoo = result(two); // should be result(null, two), but one will be passed after a 2 seconds and I want to sum them
            }
    
            second();
    
            function result(two) {
                return function foo(one) {
                    var ultraHardMath = one + two;
                    console.log(ultraHardMath);
                }
            }
        })();
&#13;
&#13;
&#13;