Iterator
一句话解释
iterator 是 对象 那个 provides 序列 的 值 one at 时间 通过 下一个() 方法.
用大白话说
iterator 是对象那个 produces 序列的 值 on demand. 它 implements iterator protocol: 对象使用 next() 方法那个返回 { value, done } — value 是下一个项 and done is true when the sequence is exhausted.
Arrays, strings, Maps, and Sets are all iterable — they have a default iterator accessible via [Symbol.iterator]. The for...的 loop uses iterators internally. You can create custom iterators by defining an object with a 下一个() method.
Iterators enable lazy evaluation — values are produced one at a time, which is memory-efficient for large or infinite sequences. Generators (function*) are a convenient way to create iterators.
示例
const arr = [1, 2, 3];
const it = arr[Symbol.iterator]();
console.log(it.next()); // { value: 1, done: false }
console.log(it.next()); // { value: 2, done: false }
console.log(it.next()); // { value: 3, done: false }
console.log(it.next()); // { value: undefined, done: true }iterator's 下一个() 返回 每个 值 in 序列, 然后 做:真 当 exhausted.
相关术语
相关资源
相关课程
- 数组
学习 关于 iteration.
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。