柯里化函数
function add(a, b, c) {
return a + b + c
}
let createCurry = (fn, ...args) => {
let arr = args || []
let length = fn.length
return (...res) {
let newArr = arr.slice(0)
newArr.push(...res)
if ( newArr.length < length) {
return createCurry.call(this, fn, ...newArr)
} else {
return fn.apply(this, newArr)
}
}
}
let curryAdd = createCurry(add)
console.log(curryAdd(3)(4)(5)) //12