迭代器Iterator

  // 有限迭代器
  function createArrayIterator(arr) {
    let index = 0;

    return {
      next() {
        if (index < arr.length) {
          return { done: false, value: arr[index++] }
        }

        return { done: true, value: undefined }
      }
    }
  }

  const names = ['a', 'b']
  const nums = [1, 2]

  const namesIterator = createArrayIterator(names);
  const numsIterator = createArrayIterator(nums);

  console.log(namesIterator.next());
  console.log(namesIterator.next());
  console.log(namesIterator.next());
  console.log(namesIterator.next());

  console.log(numsIterator.next());
  console.log(numsIterator.next());
  console.log(numsIterator.next());
  console.log(numsIterator.next());



  // 无限迭代器
  function createInfiniteArrayIterator() {
    let index = 0;

    return {
      next() {
        return { done: false, value: index++ }
      }
    }
  }

  const infiniteArrayIterator = createInfiniteArrayIterator();

  console.log(infiniteArrayIterator.next());
  console.log(infiniteArrayIterator.next());
  console.log(infiniteArrayIterator.next());
  console.log(infiniteArrayIterator.next());


  // 创建可迭代对象(什么是可迭代对象?)
  const iteratorObj = {
    names: ['a', 'b', 'c'],
    [Symbol.iterator]: function () {
      let index = 0;

      return {
        next: () => {
          if (index < this.names.length) {
            return { done: false, value: this.names[index++] }
          }
          return { done: true, value: undefined }
        }
      }
    }
  }

  const iterator = iteratorObj[Symbol.iterator]();
  console.log(iterator.next());
  console.log(iterator.next());
  console.log(iterator.next());
  console.log(iterator.next());
  console.log(iterator.next());

  // 此时iteratorObj是一个可迭代对象,像for of遍历的是一个可迭代对象,如果是普通对象({a: 1}),使用for of进行遍历时会报错
  for (const item of iteratorObj) {
    // 1. 输出的值其实是iterator.next().value
    // 2. 当done为true时, 停止遍历, 因此item的值只会输出a,b,c
    console.log(item);
  }

// 创建一个可迭代类
  class Classroom {
    constructor (name, age, students) {
      this.name = name;
      this.age = age;
      this.students = students;
    }

    entry(newStudent) {
      this.students.push(newStudent)
    }

    [Symbol.iterator]() {
      let index = 0;

      return {
        next: () => {
          if (index < this.students.length) {
            return {done: false, value: this.students[index++]}
          }

          return {done: true, value: undefined}
        },
        return: () => {
          console.log('监听到提前停止了')
          return {done: true, value: undefined}
        }
      }
    }
  }

  const classroom = new Classroom('3橦', 205, ['aaa', 'bbb', 'ccc']);
  classroom.entry('ddd')

  for (const item of classroom) {
    console.log(item);
    if (item === 'ccc') break; // 监听迭代器停掉了,在return方法内
  }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值