柯里化方法
原始方法
function sum(a, b, c, d) {
return a + b + c + d
}
console.log(sum(1, 2, 3, 4)) // 10
柯里化
function curring(fn) {
let args = []
let inner = (arr = []) => {
args.push(...arr)
console.log(args)
return args.length >= fn.length ? fn(...args) : (...args) => inner(args)
}
return inner()
}
const fn = curring(sum)
执行
fn(1)(2)(3)(4) // 10