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

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

 

posted @ 2016-02-02 13:17  Coca-code  阅读(197)  评论(0编辑  收藏  举报