滴滴面试题

2019-08-26  本文已影响0人  织雪纱奈
 class TaskLists {
            constructor() {
                this.middleware = []
            }

            add(fn, context, ...arg) {
                this.middleware.push(fn.bind(context, ...arg))
            }

            compose (middleware) {
                dispatch (0) 
                function dispatch (i) {
                    let fn = middleware[i]
                    fn(middleware[i+1]);
                }
            }

            run() {
                this.compose(this.middleware)
            }

            stop() {
                
            }
        }
        let task = new TaskLists()

        task.add(function(next){
            console.log('我是first')
            next()
        })
        task.add(function(){
            console.log('我是second')
        })
        task.run()
上一篇下一篇

猜你喜欢

热点阅读