js函数柯理化

所谓的函数柯理化,简单来说就是,一个需要接收多个参数的函数,进行分开一个个的传递参数,当函数执行的时候,传递剩余的参数。

主要作用在于增强函数的通用性。

如下举个例子:

function  custom(fn){
    var arg=Array.prototype.slice.call(arguments,1);
    console.log(arg)
    return function(){
        var otherArg=Array.prototype.slice.call(arguments);
        console.log(otherArg);
        var newArg=arg.concat(otherArg);
        return fn.apply(null,newArg)
    }
}
var mathAdd=function(num1,num2){
    return num1+num2
}
var nowMath=custom(mathAdd,10);
console.log(nowMath(5))

 

posted on 2018-12-26 16:53  心近xb  阅读(524)  评论(0编辑  收藏  举报

导航