How can I make var a = add(2)(3); //5 work?

后端 未结 28 2448
长发绾君心
长发绾君心 2020-11-27 14:11

I want to make this syntax possible:

var a = add(2)(3); //5

based on what I read at http://dmitry.baranovskiy.com/post/31797647

I\

28条回答
  •  执念已碎
    2020-11-27 14:55

    try this will help you in two ways add(2)(3) and add(2,3)

    1.)

     function add(a){ return function (b){return a+b;} }
    
        add(2)(3) // 5
    

    2.)

    function add(a,b){
            var ffffd = function (b){return a+b;};
            if(typeof b =='undefined'){
                return ffffd;
            }else{
                return ffffd(b);
            }
        }
    
    add(2)(3) // 5
    add(2,3) // 5
    

提交回复
热议问题