• js的柯里化currying


    转载:http://www.zhangxinxu.com/wordpress/2013/02/js-currying/

    我自己的理解柯里化就是这样的,(1)必须返回匿名函数,(2)参数复用。

    1. 参数复用2. 提前返回;3. 延迟计算/运行

    参数复用。比如有一个函数,我们需要一个默认的参数,当然这是扯了,我们直接在函数里直接添加一个变量每次就行了,言归正传,本来就是这样的,

    或许这只是一个抛砖引玉。

    var currying = function(fn) {
        // fn 指官员消化老婆的手段
        var args = [].slice.call(arguments, 1);
        // args 指的是那个合法老婆
        return function() {
            // 已经有的老婆和新搞定的老婆们合成一体,方便控制
            var newArgs = args.concat([].slice.call(arguments));
            // 这些老婆们用 fn 这个手段消化利用,完成韦小宝前辈的壮举并返回
            return fn.apply(null, newArgs);
        };
    };
    
    // 下为官员如何搞定7个老婆的测试
    // 获得合法老婆
    var getWife = currying(function() {
        var allWife = [].slice.call(arguments);
        // allwife 就是所有的老婆的,包括暗渡陈仓进来的老婆
        console.log(allWife.join(";"));
    }, "合法老婆");
    
    // 获得其他6个老婆
    getWife("大老婆","小老婆","俏老婆","刁蛮老婆","乖老婆","送上门老婆");
    
    // 换一批老婆
    getWife("超越韦小宝的老婆");

    2.提前返回

    我们通常写一个函数会这样子写,

    var addEvent = function(el, type, fn, capture) {
        if (window.addEventListener) {
            el.addEventListener(type, function(e) {
                fn.call(el, e);
            }, capture);
        } else if (window.attachEvent) {
            el.attachEvent("on" + type, function(e) {
                fn.call(el, e);
            });
        } 
    };

    上面的方法有什么问题呢?很显然,我们每次使用addEvent为元素添加事件的时候,(eg. IE6/IE7)都会走一遍if...else if ...,其实只要一次判定就可以了,怎么做?–柯里化。改为下面这样子的代码:

    var addEvent = (function(){
        if (window.addEventListener) {
            return function(el, sType, fn, capture) {
                el.addEventListener(sType, function(e) {
                    fn.call(el, e);
                }, (capture));
            };
        } else if (window.attachEvent) {
            return function(el, sType, fn, capture) {
                el.attachEvent("on" + sType, function(e) {
                    fn.call(el, e);
                });
            };
        }
    })();
    3.延迟计算/运行
    var curryWeight = function(fn) {
        var _fishWeight = [];
        return function() {
            if (arguments.length === 0) {
                return fn.apply(null, _fishWeight);
            } else {
                _fishWeight = _fishWeight.concat([].slice.call(arguments));
            }
        }
    };
    var fishWeight = 0;
    var addWeight = curryWeight(function() {
        var i=0; len = arguments.length;
        for (i; i<len; i+=1) {
            fishWeight += arguments[i];
        }
    });
    
    addWeight(2.3);
    addWeight(6.5);
    addWeight(1.2);
    addWeight(2.5);
    addWeight();    //  这里才计算
    
    console.log(fishWeight);    // 12.5
  • 相关阅读:
    acwing272. 最长公共上升子序列
    哈夫曼编码简单实现
    Linked List Sorting
    jmeter-线程组
    css-书写规范
    mysql-踩坑记录
    vue-npm install
    css-选择器
    js-process对象
    linux-常用命令
  • 原文地址:https://www.cnblogs.com/coding4/p/6536123.html
Copyright © 2020-2023  润新知