计算调用另一个函数的频率的函数

时间:2017-07-05 20:04:06

标签: javascript

我有两次'返回传递给它的参数2的函数。我还有另一个功能' runTwice'它计算了两次'两次'功能(这个想法是我希望'两次'功能只能运行两次'无论通过' runTwice'函数调用它的频率如何)。你能帮忙吗?

功能如下:



var count = 1;
    
function twice(num){
  return num*2;
}

function runTwice(func){
  if (count<3){
    count++;
    return func;
  } else {
    return 'Function cannot run!';
  }
}
    
var myFunc = runTwice(twice)
    
var output = [];

for (var i = 0; i < 3; i++){
  output.push(myFunc(i));
}

console.log(output);
&#13;
&#13;
&#13;

我希望输出为[0,2,&#39;功能无法运行!&#39;]。

如果算上两次&#39;我可以做这项工作。直接运作,但我希望了解为什么这不起作用如上所述。

2 个答案:

答案 0 :(得分:1)

函数runTwice应返回另一个函数,该函数将决定是调用函数func(使用Function.prototype.apply)还是返回字符串消息:

&#13;
&#13;
function twice(num){
    return num * 2;
}

function runTwice(func){
    var count = 0;                              // this will be trapped in a closure along with func
    return function() {                         // this is the function that gets called
      count++;                                  // it increments its version of the count variable
      if(count <= 2)                            // if count is less than 2
        return func.apply(this, arguments);     // then it calls the function func with whatever arguments passed into it and return the returned value of that call
      return "Not available anymore!";          // otherwise (count > 2), then it returns a string
    }
}

var myFunc = runTwice(twice);

for (var i = 0; i < 3; i++){
    console.log(myFunc(i));
}
&#13;
&#13;
&#13;

更好:

您也可以传递允许的次数:

&#13;
&#13;
function double(num) {
    return num * 2;
}

function triple(num) {
    return num * 3;
}

function run(func, times){
    var count = 0;                              // this will be trapped in a closure along with func and times
    return function() {                         // this is the function that gets called
      count++;                                  // it increments its version of the count variable
      if(count <= times)                        // if count is less than times
        return func.apply(this, arguments);     // then it calls the function func with whatever arguments passed into it and return the returned value of that call
      return "Not available anymore!";          // otherwise (count > times), then it returns a string
    }
}

var double2times = run(double, 2);              // double2times can only be called 2 times
var triple5times = run(triple, 5);              // triple5times can only be called 5 times

for (var i = 0; i < 10; i++){
    console.log("Double:", double2times(i));
    console.log("Triple:", triple5times(i));
}
&#13;
&#13;
&#13;

答案 1 :(得分:1)

为了好玩,我会制作一个通用的expireAfter(invocable[, times[, message]])函数:

function expireAfter(invocable, times = 2, message = 'Function cannot run!') {
  return function expires() {
    if (times > 0) {
      times--;

      return invocable.apply(this, arguments);
    }
    
    return message;
  }
}

function twice(n) {
  return n * 2;
}

var myFunc = expireAfter(twice);

console.log(Array(3)
  .fill()
  .map((_, index) => myFunc(index))
);