1️⃣ 迭代器 ( Iteration )

遍历器(Iterator)就是一种机制。它是一种接口,为各种不同的数据结构提供统一的访问机制。任何数据结构只要部署 Iterator 接口,就可以完成遍历操作。

  1. 1. ES6 创造了一种新的遍历命令 for...of 循环,Iterator 接口主要供 for...of 消费
  2. 2. 原生具备 iterator 接口的数据(可用 for of 遍历)
  3. 1. Array
  4. 2. Arguments
  5. 3. Set
  6. 4. Map
  7. 5. String
  8. 6. TypedArray
  9. 7. NodeList
  10. 3. 工作原理
  11. 1. 创建一个指针对象,指向当前数据结构的起始位置
  12. 2. 第一次调用对象的 next 方法,指针自动指向数据结构的第一个成员
  13. 3. 接下来不断调用 next 方法,指针一直往后移动,直到指向最后一个成员
  14. 4. 每调用 next 方法返回一个包含 value 和 done 属性的对象

注: 需要自定义遍历数据的时候,要想到迭代器。

  1. const arr = ['a', 'b', 'c']
  2. // 创建一个指针对象,指向当前数据结构的起始位置
  3. let iterator = arr[Symbol.iterator]()
  4. // 第一次调用对象的 next 方法,指针自动指向数据结构的第一个成员
  5. console.log(iterator.next()); // {value: "a", done: false}
  6. // 接下来不断调用 next 方法,指针一直往后移动,直到指向最后一个成员
  7. console.log(iterator.next()); // {value: "b", done: false}
  8. console.log(iterator.next()); // {value: "c", done: false}
  9. console.log(iterator.next()); // {value: undefined, done: true}
  10. // 每调用 next 方法返回一个包含 value 和 done 属性的对象

2️⃣ 迭代器的应用 - 自定义遍历数据

  1. const obj = {
  2. name: "字母",
  3. zm: [
  4. 'a',
  5. 'b',
  6. 'c'
  7. ],
  8. [Symbol.iterator]() {
  9. let index = 0;
  10. let _this = this;
  11. return {
  12. next: function () {
  13. if (index < _this.zm.length) {
  14. const result = { value: _this.zm[index], done: false };
  15. // 下标自增
  16. index++;
  17. // 返回结果
  18. return result;
  19. } else {
  20. return { value: undefined, done: true };
  21. }
  22. }
  23. };
  24. }
  25. }
  26. // 遍历这个对象
  27. for (let v of obj) {
  28. console.log(v);
  29. }

1️⃣ 生成器函数 ( Generator )

生成器函数是 ES6 提供的一种异步编程解决方案,语法行为与传统函数完全不同。
Generator 函数有多种理解角度。语法上,首先可以把它理解成,Generator 函数是一个状态机,封装了多个内部状态。
执行 Generator 函数会返回一个遍历器对象,也就是说,Generator 函数除了状态机,还是一个遍历器对象生成函数。返回的遍历器对象,可以依次遍历 Generator 函数内部的每一个状态。
形式上,Generator 函数是一个普通函数,但是有两个特征。一是,function关键字与函数名之间有一个星号;二是,函数体内部使用yield表达式,定义不同的内部状态(yield在英语里的意思就是“产出”)。
代码说明:

  1. 1. * 的位置没有限制
  2. 2. 生成器函数返回的结果是迭代器对象,调用迭代器对象的 next 方法可以得到 yield 语句后的值
  3. 3. yield 相当于函数的暂停标记,也可以认为是函数的分隔符,每调用一次 next 方法,执行一段代码
  4. 4. next 方法可以传递实参,作为 yield 语句的返回值
  1. function* gen() {
  2. console.log(1);
  3. yield 'hello';
  4. console.log(2);
  5. yield 'world';
  6. console.log(3);
  7. }
  8. var hw = gen();
  9. /* hw.next(); // 1
  10. hw.next(); // 2
  11. hw.next(); // 3 */
  12. console.log(hw.next());
  13. // 1
  14. // {value: "hello", done: false}
  15. console.log(hw.next());
  16. // 2
  17. // {value: "world", done: false}
  18. console.log(hw.next());
  19. // 3
  20. // {value: undefined, done: true}
  21. // 遍历
  22. for (const iterator of gen()) {
  23. console.log(iterator);
  24. // 1
  25. // hello
  26. // 2
  27. // world
  28. // 3
  29. }

2️⃣ 生成器函数参数

next 方法可以带一个参数,该参数就会被当作上一个yield表达式的返回值。

  1. function* gen(arg) {
  2. // 第一次调用
  3. console.log(arg); // AAA
  4. let one = yield 111;
  5. // 第二次调用
  6. console.log(one); // BBB
  7. let two = yield 222;
  8. // 第三次调用
  9. console.log(two); // CCC
  10. let three = yield 333;
  11. // 第四次调用
  12. console.log(three); // DDD
  13. }
  14. var hw = gen('AAA');
  15. console.log(hw.next());
  16. console.log(hw.next('BBB'));
  17. console.log(hw.next('CCC'));
  18. console.log(hw.next('DDD'));

2️⃣ 生成器函数实例 - 解决回调地狱

  1. function one() {
  2. setTimeout(() => {
  3. console.log(1);
  4. iterator.next()
  5. }, 1000);
  6. }
  7. function two() {
  8. setTimeout(() => {
  9. console.log(2);
  10. iterator.next()
  11. }, 2000);
  12. }
  13. function three() {
  14. setTimeout(() => {
  15. console.log(3);
  16. }, 3000);
  17. }
  18. function* gen() {
  19. yield one();
  20. yield two();
  21. yield three();
  22. }
  23. let iterator = gen()
  24. iterator.next()