联系:中间件挂载原理。
// 聚合函数,查看 redux 源码function fn1 (x) {console.log('fn1', x)return x}function fn2 (x) {console.log('fn2', x)return x}function fn3 (x) {console.log('fn3', x)return x}// 聚合函数function compose(...funcs) {// 防止 compose 函数,入参为空if (!funcs.length) return arg => arg// compose 函数,入参只有一个参数,立即执行if (funcs.length === 1) return funcs[0]// args 是 a 函数传入的参数// 主要传入函数的执行顺序,a , b 可相互在其内部执行:a(b(...args)) 或 b(a(...args))return funcs.reduce((a, b) => (...args) => a(b(...args)))}// var a = compose()('msg')// a // msgvar a = compose(fn1, fn2, fn3)('msg')a // fn3 msg ; fn2 msg ; fn1 msg ; msg
function fn1 (x) {return x + 1;}function fn2 (x) {return x + 2;}function fn3 (x) {return x + 3}function fn4 (x) {return x + 4}// 要求:var a = compose(fn1, fn2, fn3, fn4)// console.log(a(1)) // 1+4+3+2+1=11var a = compose(fn1, fn2, fn3, fn4)(1)a // 11
