编写一个方法sum,使sum(1,2,3) 和 sum(1,2)(3)的输出都为6

function sum(){
    var _args = Array.prototype.slice.call(arguments)
    console.log(_args)

    var add =  function(){
        _args.push(...arguments)

        return add
    }
    add.valueof = function(){
        return _args.reduce((a,b)=>{
            return a+b
        })

    }
    return add
}
console.log(sum(1,2)(3).valueof())

你可能感兴趣的:(javascript)