Skip to content

函数柯里化

js
function curry(func) {

  return function curried(...args) {
    console.log(args, 'curried----')
    if (args.length >= func.length) {
      return func.apply(this, args);
    } else {
      return function(...args2) {
        console.log(args2, 'args2----')
        return curried.apply(this, args.concat(args2));
      }
    }
  }

}

// test case
function sum(a, b, c, d) {
  return a + b + c + d
}
const cSum = curry(sum)
console.log(cSum(1)(2)(10)(20), 'result----')