Iterators & Generators in depth
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators
const arr = [11,12,13];
// (3) [11, 12, 13]
const itr = arr[Symbol.iterator]();
// Array Iterator {}
itr.next();
{value: 11, done: false}
itr.next();
{value: 12, done: false}
itr.next();
{value: 13, done: false}
itr.next();
{value: undefined, done: true}
Symbol.iterator;
// Symbol(Symbol.iterator)
function *infiniteNumbers() {
var n = 1;
while (n < 3){
yield n++;
}
}
const numbers = infiniteNumbers()
// infiniteNumbers {<suspended>}
numbers.next();
{value: 1, done: false}
numbers.next();
{value: 2, done: false}
numbers.next();
{value: undefined, done: true}
Iterators
迭代器
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
Generators
生成器
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/yield
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/yield*
for of
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of
for, for...in, for...of, foeEach, map, filter, reduce, ...
refs
https://www.barretlee.com/blog/2016/07/09/a-kickstarter-guide-to-writing-es6/
©xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!