柯里化工具函数实现,如下:
function curry(fn) {
let length = fn.length
return function curriedFn(...args) {
length -= args.length
if (length <= 0) {
return fn.apply(this, args)
} else {
return function(...argsE) {
return curriedFn.apply(this, [...args, ...argsE])
}
}
}
}
const add = (a, b, c, d) => a + b + c + d
const curriedAdd = curry(add)
curriedAdd(1)(2, 3)(4) // 10
Created 2021-03-05T16:45:01+08:00, updated 2021-03-05T17:02:48+08:00 · History · Edit