redux中compse的实现
export default function compose(...fns) {
if (fns.length == 1) {
return fns[0];
}
// 俄罗斯套娃
return fns.reduce((a, b) => (...args) => a(b(...args)));
}
// 老的作者的实现,比较好理解
function composeOld(...fns) {
return function (...args) {
// 拿到最后一个
let last = fns.pop();
fns.reduceRight((prev, next) => {
// 第一次prev = last(...args)
// 这样一层套一层,还是俄罗斯套娃
return next(prev);
}, last(...args))
}
}
function add1(str) {
return str + 1;
}
function add2(str) {
return str + 2;
}
function add3(str) {
return str + 3;
}
let add = composeOld(add3, add2, add1);
let r = add('jason');
console.log(r);
本文标题:redux中compse的实现
本文链接:https://www.haomeiwen.com/subject/clsluctx.html
网友评论