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

编程题:实现一个add方法 #16

Open
CodeRookie262 opened this issue Jan 6, 2021 · 1 comment
Open

编程题:实现一个add方法 #16

CodeRookie262 opened this issue Jan 6, 2021 · 1 comment

Comments

@CodeRookie262
Copy link
Owner

例如

function add(){//...}

add(1)(2,3)(4).value()   
输出: 10
@CodeRookie262
Copy link
Owner Author

可以利用闭包的机制保存一个变量,add 执行时对这个私有变量做操作,当调用 value() 时返回结果即可。

const add = (function () {
  let result = 0;
  function add(...nums) {
    result += nums.reduce((a, b) => a + b);
    return add;
  }

  add.value = function () {
    return result;
  };

  return add;
})();

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

No branches or pull requests

1 participant