经典面试题函数柯里化: add(1)(2)(3) = 6

function currying() {
  const args = Array.prototype.slice.call(arguments);

  const inner = function () {
    args.push(...arguments);
    return inner;
  };
  inner.__proto__[Symbol.toPrimitive] =
    inner.toString =
    inner.getValue =
      () => args.reduce((prev, next) => prev + next, 0);

  return inner;
}

const sum = currying(1)(2)(3);
console.log(sum*1);// 6

const res = currying(1, 2, 3, 4, 5)(6, 7)(8, 9)(10);
console.log(res.getValue()); // 55
console.log(res - 1); // 54
posted @ 2023-08-14 19:32  Echoyya、  阅读(17)  评论(0编辑  收藏  举报