介绍

对数据结构的读取的一种方式,有序的,连续的,基于拉取的一种消耗数据的组织方式

let arr = [1,2,3,4];
console.log(arr);

Untitled

var arr1 = [1,2,3,4,5];
var ite = arr1[Symbol.iterator]();
console.log(ite);

Untitled

var arr1 = [1,2,3,4];
var ite = arr1[Symbol.iterator]();
console.log(ite.next());
console.log(ite.next());
console.log(ite.next());
console.log(ite.next());
console.log(ite.next());
console.log(ite.next());

Untitled

部署了迭代器接口的数据结构

var tArr = new Int8Array(5);
tArr[0] = 100;

Untitled

⭐️ 实现 iterator

function myIterator(arr) {
  let index = 0;
  return {
    next() {
      return index < arr.length 
          ? { value: arr[index++], done: false }
          : { value: undefined, done: true }
    }
  }
}

var iter = myIterator([1,2,3,4,5]);
console.log(iter.next());
console.log(iter.next());
console.log(iter.next());
console.log(iter.next());
console.log(iter.next());
console.log(iter.next());

Untitled