js函数柯理化

Deadly 提交于 2020-02-08 22:00:05

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

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

如下举个例子:

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

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!