迭代器和生成器 笔记总结
迭代器(Iterator)
- 迭代器是一种特殊对象,用于提供一种顺序访问数据集合的方法。
- 迭代器对象必须实现一个
next()
方法,用于返回包含两个属性的对象:value
和done
。 value
表示当前迭代到的值,done
表示是否迭代结束。
迭代器的基本用法
// 定义一个简单的迭代器示例
function simpleIterator(arr) {
let index = 0;
return {
next: function() {
if (index < arr.length) {
return { value: arr[index++], done: false };
} else {
return { value: undefined, done: true };
}
}
};
}
const myIterator = simpleIterator([1, 2, 3]);
console.log(myIterator.next()); // Output: { value: 1, done: false }
console.log(myIterator.next()); // Output: { value: 2, done: false }
console.log(myIterator.next()); // Output: { value: 3, done: false }
console.log(myIterator.next()); // Output: { value: undefined, done: true }
可迭代对象(Iterable)
- 可迭代对象是指具有Symbol.iterator属性的对象,该属性指向一个迭代器对象。
- 可迭代对象可以被
for...of
循环遍历。 -
const iterableObj = { [Symbol.iterator]: function() { let index = 0; return { next: function() { if (index < 3) { return { value: index++, done: false }; } else { return { value: undefined, done: true }; } } }; } }; for (const item of iterableObj) { console.log(item); // Output: 0, 1, 2 }
生成器(Generator)
- 生成器是一种特殊的函数,使用
function*
语法定义,可以通过yield
关键字暂停和继续执行。 - 调用生成器函数不会立即执行代码,而是返回一个迭代器对象。
-
生成器的基本用法
-
function* simpleGenerator() { yield 1; yield 2; yield 3; } const generatorObj = simpleGenerator(); console.log(generatorObj.next()); // Output: { value: 1, done: false } console.log(generatorObj.next()); // Output: { value: 2, done: false } console.log(generatorObj.next()); // Output: { value: 3, done: false } console.log(generatorObj.next()); // Output: { value: undefined, done: true }
yield 和 yield*
yield
:在生成器函数内部使用yield
关键字可以暂停代码执行,并将yield
后面的值返回给调用者。调用next()
方法会从上一个yield
语句处继续执行代码。yield*
:在生成器函数内部使用yield*
后跟另一个生成器函数,可以将控制权交给被调用的生成器函数,直到它结束。-
function* generatorA() { yield 1; yield 2; } function* generatorB() { yield 3; yield 4; } function* combinedGenerator() { yield* generatorA(); yield* generatorB(); yield 5; } const combinedObj = combinedGenerator(); console.log(combinedObj.next()); // Output: { value: 1, done: false } console.log(combinedObj.next()); // Output: { value: 2, done: false } console.log(combinedObj.next()); // Output: { value: 3, done: false } console.log(combinedObj.next()); // Output: { value: 4, done: false } console.log(combinedObj.next()); // Output: { value: 5, done: false } console.log(combinedObj.next()); // Output: { value: undefined, done: true }
生成器的高级用法
- 可以通过
yield
和yield*
实现异步迭代,处理异步操作。 - 生成器可以用于无限序列的生成,比如斐波那契数列。
-
function* fibonacciSequence() { let prev = 0; let current = 1; while (true) { yield current; [prev, current] = [current, prev + current]; } } const fibonacciObj = fibonacciSequence(); console.log(fibonacciObj.next()); // Output: { value: 1, done: false } console.log(fibonacciObj.next()); // Output: { value: 1, done: false } console.log(fibonacciObj.next()); // Output: { value: 2, done: false } console.log(fibonacciObj.next()); // Output: { value: 3, done: false } console.log(fibonacciObj.next()); // Output: { value: 5, done: false } // 依次返回斐波那契数列的值,永远不会结束
总结
迭代器和生成器是JavaScript中处理迭代和异步编程的重要工具。迭代器提供一种顺序访问数据集合的方法,而生成器则可以通过
yield
和yield*
实现异步迭代和无限序列的生成。学会使用迭代器和生成器可以使代码更加灵活、清晰和易于维护,帮助我们解决复杂的异步编程问题。以上就是关于迭代器和生成器的所有核心知识点的详细总结,希望对你深入理解JavaScript中这两个重要概念有所帮助。