Redux中间件applyMiddleware核心思想
function a(next){ return function(action){ // console.log(next); //这里的next为b函数的返回值 action = action + 1; next(action); } } function b(next){ return function(action){ // console.log(next);//这里next为初始的dispatch action = action + 1; next(action); } } let arr = []; arr.push(a); arr.push(b); let compose = arr.reduce(function(prev,curr){ return function(next){ return prev(curr(next)) } }); var globalV = 0; //定义一个全局变量,用来计数 //初始的dispatch let dispatch = function(action){ //初始功能 globalV += action; } //加强后的dispatch let strendispatch = compose(dispatch); strendispatch(1); console.log(globalV); //结果是 3