🔰iterator
object that can produce the next iteration result.
has next() method that returns an iteration result. (similar to iterator objects conforming to IteratorProtocol in Swift)
iterators:
str.matchAll() - returns an iterator of matching results.
objects returned by generator functions.
iterators don't always run all the way to the end:
a for-of loop might be terminated with a break, return, or an exception.
when destructuring an iterable, the next() method (of an iterator) is only called enough times to obtain values for each variable.
All iterator protocol methods
return() - give iterator a chance to do "cleanup" actions.
throw()
are expected to return an iteration result object.
features
next() - returns an iteration result.
return() - give iterator a chance to do cleanup actions.
iterators only iterate once❗️ - iterators only iterate once❗️
an iterator can be infinite.
use cases
iterables use make-iterator method to make iterators.
defining iterators
extending iterators
Iterator - extension of built-in iterators ⭐️
IteratorPrototype - prototype of all built-in iterators.
tyes of iterators
iterable iterator - an iterator that is itself iterable.
infinite iterator - iterator can be infinite.
str.matchAll() returns an iterator of all matching results. (⭐️ ES2020)
Last updated
Was this helpful?