ChickenDreamFactory / fe-chicken

✨✨✨ 集锦 前端JavaScript 手写题,编程题,Not just for interviews

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

7.函数柯里化

webVueBlog opened this issue · comments

// 思路: 比较函数参数,不足返回函数等待接收参数,够了就执行
function curry(fn, ...args) {
  //  比较参数
  if (args.length < fn.length) {
    //  返回函数 等待接收参数
    return function (...args2) {
      return curry(fn, ...args, ...args2)
    }
  }
  //  函数参数够了 执行该函数返回结果
  return fn.apply(this, args)
}

// 使用
function sum(a, b, c, d) {
  return a + b + c + d
}
const curriedSum = curry(sum, 1) // 保存函数和参数
const res = curriedSum(2) // 保存第2个参数与sum函数
const res2 = res(3) // 保存第3个参数
// 传进第四个参数 参数足够了 执行函数 返回执行结果 不能再传递参数了
const res3 = res2(4)
console.log('res', curriedSum, res, res2, res3)