Iterator & Generator
Iterator
an iterator is any object which implements the Iterator protocol by having a
next()
method that returns an object with two properties:
value
The next value in the iteration sequence.
done
This istrue
if the last value in the sequence has already been consumed. Ifvalue
is present alongsidedone
, it is the iterator's return value.
Iterable
满足 Iterable Protocol 的对象可以被 for of 遍历。
In order to be iterable, an object must implement the
@@iterator
method, meaning that the object (or one of the objects up its prototype chain) must have a property with a@@iterator
key which is available via constantSymbol.iterator
.
Iterable Protocol 要求通过取 [Symbol.iterator] 属性应当可以获得一个函数(也就是说这是它的一个成员方法),这个函数返回一个 iterator。
Generator
Reference
[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators
[2] https://www.cnblogs.com/xiaohuochai/p/7253466.html
[3] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
Last updated
Was this helpful?