JS手写柯里化
//支持多参数传递
function curry(fn, ...args){
let len = fn.length;
args = args || [];
let _this = this;
return function(){
let newArgs = [...args, ...arguments];
if(newArgs.length < len){
return curry.call(_this, fn, ...newArgs);
}else{
return fn.apply(_this, newArgs);
}
}
}
调用
function add(a, b, c) {
return a + b + c
}
let addMethod = curry(add)
addMethod(1)(2)(3)