函数柯里化

/**
* Created by lei.wang on 2017/1/22.
*/

'use strict';

function plus(num) {
var adder = function () {
var _args = [];
var _adder = function _adder() {
[].push.apply(_args, [].slice.call(arguments));
return _adder;
};

_adder.toString = function () {
return _args.reduce(function (a, b) {
return a + b;
});
}

return _adder;
}

return adder()(num);
}

module.exports = plus;

2.函数二 

<script type="text/javascript">
	var fn = function(a,b,c) {
    return a+b+c;
}

function curry(fn) {
    var arr = [],
    mySlice = arr.slice
    fnLen = fn.length;

    function curring() {
        arr = arr.concat(mySlice.call(arguments));
        if(arr.length < fnLen) {
            return curring;
        }
        return fn.apply(this, arr);
    }
    return curring
}
console.log(curry(fn)(1)(2)(3));
//6
</script>

  

posted on 2018-01-30 11:10  童彪  阅读(161)  评论(0编辑  收藏  举报

导航