201602021314_《Javvascrip柯里化(类似缓存)》

var currying = function(fn){
     var args = [];

     return function(){
          if(arguments.length === 0){
               return fn.apply(this,args);
          }else{
         [].push.apply(args,arguments);
          return arguments.callee;
          }
     }
};

var cost = (function(){
     var money = 0;
    
     return function(){
    for(var i = 0,l = arguments.length;i<l;i++){
          money+=arguments[i];
          }
     return money;
     }
})();
var cost = currying(cost);cost(1); cost(2);cost(7);
console.log(cost());  //10

 

你可能感兴趣的:(201602021314_《Javvascrip柯里化(类似缓存)》)