Iterator
Introduction
The iterator protocol defines a standard way to produce a sequence of values and potentially a return value when all values have been generated.
It is mainly compose by
next
,done
,value
andreturn
Next: go to the next step
Value: obtain the value
Done: the flag for labelling finishing reason
Return: the final value
Generator function does not execute its body immediately and return an object, the function can be paused and resumed easily
Each yield statement is just like a break point to pause, the logic above the yield statement will be triggered if the next of the object is called to resume
Inheritance
You can create new generate function based on the existing function
Built-in Iterable Object
In order to be iterable, an object must implement the
[Symbol.iterator]()
method.
Last updated
Was this helpful?