企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
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 ```