简体   繁体   English

将带有预设参数值的匿名函数作为参数传递

[英]Passing an anonymous function as a parameter with preset parameter values

I ran into this by accident and am not sure how my 'popAndGo' function is working. 我偶然遇到了这个问题,并且不确定我的“ popAndGo”功能如何工作。

function Calculator(){
  var stack = [];

  var popAndGo = function(performer){
    var firstPop = stack.pop();
    var secondPop = stack.pop();
    var result = performer(firstPop, secondPop);
    stack.push(result);
  };

  this.push = function(num){
    stack.push(num);
  };
  this.value = function(){
    return stack[stack.length -1];
  };
  this.plus = function(){
    popAndGo(function(first, second){
      return first + second;
    });
  };
 }

I was practicing making my code follow DRY practices, and created this popAndGo function. 我正在练习使我的代码遵循DRY做法,并创建了这个popAndGo函数。 It takes an anonymous function and calls this function after it collects two parameters (check out the plus function). 它使用一个匿名函数并在收集两个参数后调用此函数(签出plus函数)。

I am not sure how these parameters are working. 我不确定这些参数的工作方式。 I understand parameters in general, they are basically placeholders for actual arguments you eventually pass the function. 我一般理解参数,它们基本上是最终传递给函数的实际参数的占位符。

But in the case of this.plus I am passing an anonymous function with two parameters. 但是在this.plus的情况下,我要传递一个带有两个参数的匿名函数。 How are they then taking place taking place of performer(firstPop, secondPop)? 那么他们如何代替表演者(firstPop,secondPop)呢? I visualize it working something like this: 我将其可视化为如下所示:

  var popAndGo = function(performer){
    var firstPop = stack.pop();
    var secondPop = stack.pop();
    var result = performer(firstPop, secondPop);
    stack.push(result);
  };

  this.plus = function(){
    popAndGo(function(first, second){
      return first + second;
    });
  };
 }

 // CHECK the parameters on this popAndGo, this is how I picture this working.

 var popAndGo = function(function(first, second){ 
    var firstPop = stack.pop();
    var secondPop = stack.pop();
    var result = function(first, second);
    stack.push(result);
  };

These values do not match. 这些值不匹配。 If anyone can explain how this function is being passed into my popAndGo function and being matched to the values it would clear up a lot of confusion I am having, thanks! 如果有人可以解释此函数如何传递到我的popAndGo函数中并与值匹配,它将消除我所遇到的许多混乱,谢谢!

Test Cases I am writing this code for: 测试用例我正在为以下目的编写代码:

// See http://en.wikipedia.org/wiki/Reverse_Polish_notation
describe("Calculator using reverse polish notation", function() {
  var calculator;

  beforeEach(function(){ 
    calculator = new Calculator();
  });

  it("adds two numbers", function() {
    calculator.push(2);
    calculator.push(3);
    calculator.plus();
    expect(calculator.value()).toEqual(5);
  });

  it("adds three numbers", function() {
    calculator.push(2);
    calculator.push(3);
    calculator.push(4);
    calculator.plus();
    expect(calculator.value()).toEqual(7);
    calculator.plus();
    expect(calculator.value()).toEqual(9);
  });
}

There are more tests and more functions I had to write for it. 我必须为此编写更多的测试和更多的功能。 In every function I was popping 2 values from the stack then pushing the total back on. 在每个函数中,我从堆栈中弹出2个值,然后将总数推回去。 I wanted to write a function where I did this so I wasn't constantly repeating myself. 我想编写一个函数来执行此操作,因此我不会经常重复自己。

this.plus = function(){
    popAndGo(function(first, second){ 
      return first + second;
    });
};

The function that you pass as the argument to popAndGo is an anonymous function with two parameters. 您作为popAndGo的参数传递的函数是带有两个参数的匿名函数。 That anonymous function gets bound to the parameter performer in popAndGo . 该匿名函数将绑定到popAndGo的参数performer popAndGo

When calling performer with the values of firstPop and secondPop these get bound to the parameters first and second in the anonymous function. 当使用firstPopsecondPop的值调用performer ,这些值将绑定到匿名函数中的firstsecond参数。 The anonymous function's body gets executed returning the sum of the arguments. 匿名函数的主体将执行并返回参数的总和。

Maybe you will visualize it better this way: 也许您可以通过以下方式更好地可视化它:

var popAndGo = function(){
    var performer = function(first, second){
        return first + second;
    };
    var firstPop = stack.pop();
    var secondPop = stack.pop();
    var result = performer(firstPop, secondPop);
    stack.push(result);
};

Or, simplifying, 或者,简化

var popAndGo = function(){
    var firstPop = stack.pop();
    var secondPop = stack.pop();
    var result = firstPop + secondPop;
    stack.push(result);
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM