const add = (a, b, c) => a + b + c;
const curry = (fn) => {
let args = [];
//返回一个函数
return function temp(...newArgs) {
args.push(...newArgs);//储存数据
//数组长度和函数形参个数一致
if (args.length === fn.length) {
const val = fn.apply(this, args); //使用fn的方法添加数据
args = [];
return val; //返回最后添加的值
} else {
return temp;
}
}
}
const curryAdd = curry(add);
console.log(curryAdd(1)(2)(3)); // 6
使用函数柯里化实现add(1)(2)(3)
最新推荐文章于 2023-01-11 14:06:47 发布