手写curry函数
发布时间:2022-10-25 22:32:55 1145
相关标签:
let curry = (fn, ...args) => {
let lens = fn.length
let _args = args || []
return (...lastArgs) => {
_args.push(...lastArgs)
if (_args.length === lens) {
return fn(..._args)
} else {
// 要注意的事当...会自动将参数转为数组,下次递归过程中要将数组展开
return curry(fn,..._args)
}
}
}
var fn = curry(function (a, b, c) {
console.log([a, b, c]);
});
fn("a", "b","c") // ["a", "b", "c"]
fn("a", "b")("c") // ["a", "b", "c"]
fn("a")("b")("c") // ["a", "b", "c"]
fn("a")("b", "c") // ["a", "b", "c"]
文章来源: https://blog.51cto.com/u_15440725/5787878
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报