Skip to content

函数柯里化-示例一-代码修复 #43

@Ra-Liz

Description

@Ra-Liz

这里的示例代码运行传参多一个,且不能达到预期效果。
image

建议修改成:

const currying = function (fn, ...args) {
  const len = fn.length;
  args = args || [];
  return (...arguments) => {
    const totalArgs = [...args].concat([...arguments]);

    return totalArgs.length >= len
      ? fn.call(this, ...totalArgs)
      : currying.call(this, fn, ...totalArgs);
  };
};

const sum = (a, b, c) => a + b + c;

const newSum = currying(sum);

const res = newSum(1)(2)(3) // 6

console.log(res); 

运行结果:
image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions