ES6 Iterator
Author: 图恩Category: 编程开发Views: 712Published: 2022-05-19 **Article Class: "_2rhmJa"**
---
### **What is an Iterator?**
An **interface** that provides a unified access mechanism for various data structures.
Any data structure that implements the `Iterator` interface can perform **traversal operations** (i.e., sequentially process all members of the data structure).
---
### **Functionality**
- **Unifies access** for various data structures
- **Allows sequential ordering** of data structure members
- **ES6 introduces** a new traversal command `for...of` loop, with the `Iterator` interface primarily designed for consumption by `for...of` loops.
---
### **Default `Iterator` Interface**
In **ES6**, the default `Iterator` interface is defined on the `Symbol.iterator` property.
A data structure is considered **iterable** if it has the `Symbol.iterator` property.
The `Symbol.iterator` property itself is a **function** that generates the default traversal pointer.
When executed, it returns a **traversal pointer**.
The property name `Symbol.iterator` is an **expression** returning a `Symbol` object's `iterator` attribute, a pre-defined value of type `Symbol`.
---
### **Features**
- **Core Feature**: A **pointer object** with a `next` method.
- Each call to `next` returns an **information object** containing `value` (current member's value) and `done` (boolean indicating if traversal is complete).
- `value` is the value of the current member, and `done` is a boolean indicating whether traversal has ended.
- **Additional Methods**:
- `next` (required)
- `return` (optional)
- `throw` (optional)
- **`return` method** returns an **object** (required by `Generator` syntax).
- **Use cases**:
- `for...of` loop to prematurely exit (e.g., due to error or `break` statement)
- Resource cleanup or release before traversal completes
- **`throw` method** is primarily used with `Generator` functions; typical traversal pointers do not use it.
---
### **Simple Implementation**
Use a **`Generator` function**:
```javascript
let obj = {
* [Symbol.iterator]() {
yield 'hello';
yield 'world';
}
};
for (let x of obj) {
console.log(x);
}
```
---
### **Built-in Types**
- `Array`
- `Map`
- `Set`
- `String`
- `TypedArray`
- `arguments` object of a function
- `NodeList` object
---
### **Scenarios**
- **Destructuring assignment**: Default call
- **Spread operator (`...`)**
- `yield*`
- **Other contexts where arrays are used as parameters**
- `for...of`
- `Array.from()`
- `Map()`, `Set()`, `WeakMap()`, `WeakSet()` (e.g., `new Map([['a', 1], ['b', 2]])`)
- `Promise.all()`
- `Promise.race()`
---
### **References**
- [Symbol.iterator](https://links.jianshu.com/go?to=https%3A%2F%2Fdeveloper.mozilla.org%2Fzh-CN%2Fdocs%2FWeb%2FJavaScript%2FReference%2FGlobal_Objects%2FSymbol%2Fiterator)
- [ES6 R入门 Iterator 和 for...of 循环](https://links.jianshu.com/go?to=https%3A%2F%2Fes6.ruanyifeng.com%2F%23docs%2Fiterator)
---
**Note**: The article maintains its original structure, technical terminology, and formatting while ensuring clarity and professional tone.