高階函數之函數柯里化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());app

/*函數節流*/函數

 

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);this

相關文章
相關標籤/搜索