函数柯里化

function add(a,b,c){
    return a+b+c
}
function multi(a,b,c,d){
            return a*b*c*d
        }

function curry(fn){
    var arr=[]
    var ceshi=function(){
        if(arguments.length===fn.length){
            return fn.apply(null,arguments)
        }else{
            arr=arr.concat([].slice.call(arguments))
            if(arr.length==fn.length){
                var result= fn.apply(null,arr)
                arr=[]
                return result
            }
        }
        return ceshi
    }
    return ceshi
}
const addEx=curry(add)
console.log(addEx(1,2,3)===6)  //=>true
console.log(addEx(1,2)(3)===6)  //=>true
console.log(addEx(1)(2,3)===6)  //=>true
console.log(addEx(1)(2)(3)===6)  //=>true


const multiEx=curry(multi)

console.log(multiEx(2,3,4,5)===120)  //=>true
console.log(multiEx(2,3,4)(5)===120)  //=>true
console.log(multiEx(2,3)(4,5)===120)  //=>true
console.log(multiEx(2)(3)(4)(5)===120)  //=>true

你可能感兴趣的:(函数柯里化)