Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Ra-Liz opened this issue Mar 4, 2024 · 0 comments
Open

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

Ra-Liz opened this issue Mar 4, 2024 · 0 comments

Comments

@Ra-Liz
Copy link

Ra-Liz commented Mar 4, 2024

这里的示例代码运行传参多一个,且不能达到预期效果。
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant