没啥太多东西,记录下自己实现的思路,还没看源码。之后有机会看下源码比较下实现的差异。
var App = {
index: 0,
functionList:[],
callback (ctx) {
console.log(ctx.name);
},
use (middleware) {
this.functionList.push(middleware);
},
go (ctx = {}) {
let _this = this;
let next = ()=>{
if(_this.index< _this.functionList.length){
_this.index = _this.index+1;
_this.functionList[_this.index-1](ctx,next);
}else{
_this.callback(ctx);
}
};
next();
}
};
App.use(function (ctx, next) {
ctx.name = 'hello';
next();
});
App.use(function (ctx, next) {
ctx.name += ' world';
next();
});
App.go();