如何理解这个高阶函数闭包发生了什么

How to understand what is happening with this higher order function closure

我知道我下面的代码是错误的,但显然我遗漏了一些信息。我必须编写更复杂的高阶函数,但如果我不真正理解简单的函数,我就没有机会了。

hof.add 应该 return 传递的两个参数的总数。很简单的... 但是考虑到我要创建这个高阶函数必须使用闭包。


    hof.add = function(add) {
      function makeAdd(a, b) {
        return add(a + b);
      }
      return makeAdd;
    };

it('returns total of the two arguments', () => {
        expect(hof.add(56, 5)).to.be.equal(56 + 5);
        expect(hof.add(91, -71)).to.be.equal(91 + -71);
      });

(在其之上构建的框架)

hof.add = function() { };

为什么要在返回的函数中创建一个函数,而不是最初只创建正确的函数?我会这样做:

hof.add = function(a, b) {
    return (a + b);
};

高阶函数 (HOF) 只是接受函数作为参数或 returns 另一个函数的函数的奇特名称。如果我更好地理解你,你正在尝试创建一个可用于添加数字的加法器函数。使用您的示例 hof.add 是一个接收函数的 HOF,returns 是另一个可用于将两个数字相加的函数

 hof.add = function(add) {
  function addFunc(a, b) {
    return add(a, b);
  }
  return addFunc;
}

function add(a, b){
  return a + b
}

const addNumbers = hof.add(add);
addNumbers(3, 14)
//17 as answer
addNumbers can recieve any two numbers and it will add them together.

add 是一个将两个数字相加的函数,它作为 hof.add 中的参数接收。 hof.add 函数 returns 一个名为 addFunc 的函数,该函数依次接收两个要相加的参数。干杯

如果你只想使用一个参数名称,也许你打算解构它:function(...add)

const not_hof = {};

// Cheating by using destructuring (...add) 
// and not a HOF since accepts only Numbers as arguments 
not_hof.add = function(...add) {  
  const makeAdd = add.reduce((prev, curr) => {
    return prev + curr;
  }, 0);
  return makeAdd; // and still not a HOF since it returns a Number 
};

console.log(not_hof.add(2, 3));              // 5
console.log(not_hof.add(9, 1, 10));          // 20
console.log(not_hof.add(1, 1, 1, 1, 1, 1));  // 6

PS:上述函数也可以表示为:

not_hof.add = (...add) => add.reduce((prev, curr) =>  prev + curr, 0);

不是 HOF(高阶函数)

虽然很多人会说上面是 高阶函数 - 因为它 return 是 Array.prototype.reduce,它实际上 不是 :

Wikipedia - Higher order function

In mathematics and computer science, a higher-order function is a function that does at least one of the following:

  • takes one or more functions as arguments (i.e. procedural parameters),
  • returns a function as its result.

但是add不是过程函数参数;即:

procedure P(f):
return f(2,3) * f(9,1)

不是return函数;而是 Number returned by Array.prototype.reduce.

1。 HOF - 一个或多个函数作为参数

至少传递一个函数作为参数:

const helper = { // Helper functions
  add(a, b) { return  Number(a) + Number(b); },
};


const hof = {};
hof.add = function(fn, add1, add2) { // HOF since it takes a function as argument
  return fn(add1, add2);             // (Returns a Number)
};

// ...But it takes three arguments
console.log(hof.add(helper.add, 56, 5)); // 61

2。 HOF - Return 函数

至少return一个函数:

const hof = {};

hof.add = function(add) { // (Takes a Number argument)
  function makeAdd(b) {
    return add + b;
  }
  return makeAdd;         // HOF since it returns a function
};

// But accepts a single argument at each call
console.log(hof.add(56)(5)); // 61

或喜欢

const hof = {};
hof.add = function(add1, add2) { // NOT LIKE YOUR EXAMPLE, 2 arguments are expected!
  function makeAdd() {
    return add1 + add2;
  }
  return makeAdd; // HOF since it returns a function
};

// ...The function had to be executed ()
console.log(hof.add(56, 5)());     // 61

但在这种情况下,它会失败,如您的测试所述:

it('returns total of the two arguments', () => { // nope :( only one argument here...

HOF 闭包

允许使用 Function.prototype.toString() 多次调用函数,以便 return 在最后一次调用字符串

const hof = {};

hof.add = function(add) {
  let sum = add;    // Store in local scope
  const makeAdd = (b) => {
    sum += b;       // Access to lexical environment variables
    return makeAdd; // Returns a function (self)  
  }
  makeAdd.toString = () => sum;
  return makeAdd;   // HOF since we return a function
}

const result = hof.add(1)(2)(2)(56);
console.log(result)         // (f) 61
console.log(typeof result); // function
console.log(result == 61)   // true
console.log(result === 61)  // false
// A legitimate test might break since the strict equality operator fails.

让我们保持简单。没有关闭,没有 HOF

如果不需要使用数组解构和 Array.prototype.reduce() 的第一个示例,那么只需坚持最简单的函数声明形式:

const not_hof = {
  add: (a, b) => a + b,
  sub: (a, b) => a - b,
  // etc...
};

console.log(  not_hof.add(56, 5)  ); // 61