函数柯里化

const person = {
  add(a, b, c) {
    console.log(this);
    return a + b + c;
  },
};
// 函数柯里化
function curry(fn) {
  function curried(...args) {
    if (args.length >= fn.length) {
      return fn.apply(this, args);
    } else {
      function curried2(...args2) {
        return curried.apply(this, [...args, ...args2]);
      }
      return curried2;
    }
  }
  return curried;
}

const test = curry(person.add);
console.log(test.call({}, 1, 2, 3));

你可能感兴趣的:(函数柯里化)