js 实现 add (1)(2)(3)(4)(5)

413 阅读1分钟
function add() {
  let args = Array.prototype.slice.call(arguments)
  if (args.length === 5) {
    return args.reduce((prev, item) => {
      return prev + item
    }, 0)
  } else {
    return function _() {
      let arg = Array.prototype.slice.call(arguments)
      args = args.concat(arg)
      if (args.length === 5) {
        return args.reduce((prev, item) => {
          return prev + item
        }, 0)
      } else {
        return _
      }
    }
  }

}
console.log(add(1, 2, 3, 4, 5));
console.log(add(1)(2, 3)(4, 5));
console.log(add(1)(2)(3)(4)(5));