JS手写代码之柯里化

柯里化函数

柯里化可以利用函数和不同的参数构成功能更加专一的函数。

柯里化其实就是利用闭包的技术将函数和参数一次次缓存起来,等到参数凑够了就执行函数。

function curry(fn, ...rest) {
  const length = fn.length
  return function() {
    const args = [...rest, ...arguments]
    if (args.length < length) {
      return curry.call(this, fn, ...args)
    } else {
      return fn.apply(this, args)
    }
  }
}

一个幽默的前端爱好者,记录下自己的心得体会
原文地址:https://www.cnblogs.com/little-oil/p/15066182.html