JavaScript 中的迭代器、生成器与函数的多重角色
1. 迭代器与迭代协议
1.1 迭代器的独立性
迭代器可以独立地遍历数组。例如:
// read two pages with it1:
it1.next(); // { value: "Twinkle, twinkle, little bat!", done: false }
it1.next(); // { value: "How I wonder what you're at!", done: false }
// read one page with it2:
it2.next(); // { value: "Twinkle, twinkle, little bat!", done: false }
// read another page with it1:
it1.next(); // { value: "Up above the world you fly,", done: false }
在这个例子中,两个迭代器 it1 和 it2 独立地按照各自的进度遍历数组。
1.2 迭代协议
迭代协议允许任何对象成为可迭代对象。只要类提供了 Symbol.iterator 方法,该方法返回一个具有 next 方法的对象,且 next 方法返回一个包含 value 和
超级会员免费看
订阅专栏 解锁全文

被折叠的 条评论
为什么被折叠?



