console.log(sum(1, 2, 3, 4, 5).valueOf()) // => 15console.log(sum(1, 2)(3, 4)(5).valueOf()) // => 15console.log(sum(1, 2)(3)(4)(5).valueOf()) // => 15console.log(sum(1)(2)(3)(4)(5).valueOf()) // => 15复制代码
/** * @author shao * @date 2018/03/05 * @project test */let sum = function () { let prevSum = [...arguments].reduce((a, b) => a + b) let result = function () { return sum(...([...arguments].concat(prevSum))) } result.valueOf = function () { return prevSum } return result }console.log(sum(1, 2, 3, 4, 5).valueOf()) // => 15console.log(sum(1, 2)(3, 4)(5).valueOf()) // => 15console.log(sum(1, 2)(3)(4)(5).valueOf()) // => 15console.log(sum(1)(2)(3)(4)(5).valueOf()) // => 15复制代码