多元函数转换成为一元函数
const curry = function(fn) {
return function curriedFn(...args) {
if(args.length<fn.length) {
return function () {
return curriedFn(...args.concat([...arguments]))
}
}
return fn(...args)
}
}
function test(x, y, z) {
return x+y+z
}
console.log(test.length)// 参数的多少:3
let myFn = curry(test)
// myFn(1)(2)(3)
let res = myFn(1)(2)(3)
console.log(res)// 6