interface Iterator<T> {
next(): IteratorResult<T>;
}
interface IteratorResult<T> {
value: T;
done: boolean;
}
let numbers = [1, 2, 3];
let iterator = numbers[Symbol.iterator]();
console.log(iterator.next()); // 输出:{ value: 1, done: false }
console.log(iterator.next()); // 输出:{ value: 2, done: false }
console.log(iterator.next()); // 输出:{ value: 3, done: false }
console.log(iterator.next()); // 输出:{ value: undefined, done: true }
function* generateNumbers() {
yield 1;
yield 2;
yield 3;
}
let generator = generateNumbers();
console.log(generator.next()); // 输出:{ value: 1, done: false }
console.log(generator.next()); // 输出:{ value: 2, done: false }
console.log(generator.next()); // 输出:{ value: 3, done: false }
console.log(generator.next()); // 输出:{ value: undefined, done: true }
本文为翻滚的胖子原创文章,转载无需和我联系,但请注明来自猿教程iskeys.com