高阶函数之函数柯里化function currying

var cost = (function(){
    var args = [];
    return function(){
        if(arguments.length === 0){
        var money = 0;
        for(var i=0,l=args.length; i<l; i++){
            money += args[i];
        }
        return money;
        }else{
            [].push.apply(args,arguments);
        }
    }
})();
cost(100);
cost(200);
cost(300);
console.log(cost());

/*函数节流*/

 

var throttle = function(fn,interval){
    var _self = fn,
        timer,
        firsttime = true;
    return function(){
        var args = arguments,
            _me = this;
        if(firsttime){
            _self.apply(_me,args);
            return  firsttime = false;
        }
        if(timer){return false;}
        timer = setTimeout(function(){
            clearTimeout(timer);
            timer = null;
            _self.apply(_me,args);
        },interval || 500);
    }
}

window.onresize = throttle(function(){
    console.log(1);
},5000);

 

posted on 2015-09-01 16:43  小武爷  阅读(261)  评论(0编辑  收藏  举报