1.什么是Iterator?
Iterator又叫做迭代器, 是一种接口
-
这里的接口和现实中接口一样, 是一种标准一种规范
例如: 电脑的USB接口有电脑USB接口的标准和规范, 正式因为有了标准和规范
所以A厂商生成的USB线可以插到B厂商电脑的USB接口上 -
它规定了不同数据类型统一访问的机制, 这里的访问机制主要指数据的遍历
在ES6中Iterator接口主要供for...of消费
2.默认情况下以下数据类型都实现Iterator接口
- Array/Map/Set/String/TypedArray/函数的 arguments 对象/NodeList 对象
3.Iterator接口特点
- 1.只要一个数据已经实现了Iterator接口, 那么这个数据就有一个叫做[Symbol.iterator]的属性
- 2.[Symbol.iterator]的属性会返回一个函数
- 3.[Symbol.iterator]返回的函数执行之后会返回一个对象
- 4.[Symbol.iterator]函数返回的对象中又一个名称叫做next的方法
- 5.next方法每次执行都会返回一个对象{value: 1, done: false}
- 6.这个对象中存储了当前取出的数据和是否取完了的标记
4.Iterator接口封装
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | class MyArray{ constructor(){ for(let i = 0; i < arguments.length; i++){ this[i] = arguments[i]; } this.length = arguments.length; } [Symbol.iterator](){ let index = 0; let that = this; return { next(){ if(index < that.length){ return {value: that[index++], done: false} }else{ return {value: that[index], done: true} } } } } } |
5.应用场景:解构赋值和扩展运算符
1 2 3 4 5 6 7 8 9 10 11 12 13 | // 1.解构赋值 // let arr = [1, 3]; let arr = new MyArray(1, 3); let [x, y, z] = arr; console.log(x, y, z); // 2.扩展运算符 // let arr1 = [1, 3]; // let arr2 = [2, 4]; let arr1 = new MyArray(1, 3); let arr2 = new MyArray(2, 4); let arr3 = [...arr1, ...arr2]; console.log(arr3); |