// 第一种:闭包存储,遍历执行
function compose1 (arr) {
return (param) => {
for (let fn of arr) {
param = fn(param);
}
return param;
}
}
// 第二种:递归嵌套,依次执行
function compose2 (arr) {
return arr.length <= 1 ? arr[0] : (...args) => compose2(arr.slice(1))(arr[0](...args))
}
// 第三种:累加嵌套,依次执行,跟第二种相似
const compose3 = (arr) => arr.reduce((res, cur) => (...args) => cur(res(...args)));
function a (p) {
return p + 1;
}
function b (p) {
return p + 6;
}
function c (p) {
return p * 2;
}
const fn = compose1([a, b, c])
console.log(fn(1))