一、迭代器模式在Redux源码中的使用

二、迭代器模式在Koa2源码中的使用

场景一、二都是利用迭代器模式,依次调用中间件,从而实现各个框架中的洋葱模型。
三、 迭代器模式在Webpack源码中的使用

简单解释下,实参modulesData是从入口文件出发,通过递归分析得到的,所有依赖文件的集合,其格式如下:

四、迭代器模式在es6中是使用
es6中之所以可以实现数组的扩展运算符以及for..of循环,是因为其内部定义了一个[Symbol.iterator]属性,这个属性的value是一个函数(迭代函数),其大致实现如下:

五、其他
这里只是举几个简单的例子,其实迭代器模式在前端领域还有非常广泛的应用,比如vue-router中各个导航守卫的依次调用、async&await函数的实现等。
网友评论