zoukankan      html  css  js  c++  java
  • 高阶函数之函数柯里化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);

  • 相关阅读:
    MySQL基础(一):检索数据
    Go基础(九):并发
    Go基础(八):反射
    Go基础(七):接口
    Go基础(六):方法
    Go基础(五):结构体
    Go基础(四):流程控制和函数
    Go基础(三):内置基础类型和一些技巧
    Go基础(二):变量和常量
    Go基础(一):命令行操作
  • 原文地址:https://www.cnblogs.com/junwu/p/4776103.html
Copyright © 2011-2022 走看看