m1=function(next){
return function(action){
console.log("m1")
next(action)
}
}
m2=function(next){
return function(action){
console.log("m2")
next(action)
}
}
dispatch=function(action){
console.log(action)
return action
}
m1 m2代表中间件,手写调用dispatch方法即
m1(m2(dispatch))({action:1})
通过applyMiddle方法实现原理
applayMiddle=function(...args){
args.unshift(dispatch)
return args.reduce(function(dispatch,item){
return item(dispatch)
})
}
所以增强的dispatch相当于
增强的dispatch==applayMiddle(m1,m2)
网友评论