🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
const add = (x) => x + 1; const mul = (x) => x * 3; const div = (x) => x / 2; div(mul(add(add(0)))); //=>3 compose(div, mul, add, add) 把类似于f(g(h(x)))这种写法简化成compose(f, g, h)(x) ```js const add = (x) => x + 1 const add2 = x => x + 2 const add3 = x => x + 3 function compose(...funcs) { return function (val) { return funcs.reverse().reduce((pre, cur, index, arr) => { return cur(pre) }, val) } } let result = compose(add)(5); console.log(result); // 11 ```