合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
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 ```