function add() { let args = [].slice.call(arguments) return function newAdd() { let sum = 0 let newArgs = [].slice.call(arguments) if (newArgs.length === 0) { args.forEach((val) => { sum += val }) return sum } else { args = args.concat(...newArgs) return newAdd } } } let a = add(1, 2)(3, 4)(5) console.log(a())