When I saw the knowledge points that I couldn't understand and use when I came to the interview questions, I asked the boss for help.

implement such a function

foo (1) 2) / / 3
foo (2) 3) (4) 5) / / 14
foo (2) (2) (2) (8) / / 14

Apr.05,2021

function foo(...args){
    let sum=0;
    for(const value of args){
        sum+=value;
    }
    function result(...args1){
        return foo(sum,...args1);
    }
    result.valueOf=function(){return sum};
    return result;
}
foo(1,2)+0
foo(2,3)(4,5)+0
foo(2)(2)(2)(8)+0

// ES5
function add(){
    var args = [].slice.apply(arguments)
    
    function result() {
        return add.apply(null, args.concat([].slice.apply(arguments)))
    }
    
    result.valueOf = function() {
        return args.reduce(function(sum, x) {
            return sum + x
        }, 0)
    }
    
    return result
}
Menu