Itaretors & Generators
4 Ara 2016Iterators
In JavaScript an iterator is an object that provides a next() method which returns the next item in the sequence.
String, Array, TypedArray, Map and Set are all built-in iterables, because the prototype objects of them all have a Symbol.iterator method.1234567let myArray = [1,2];let iterator = myArray[Symbol.iterator]();console.log(iterator.next()); // { done: false, value: 1 }console.log(iterator.next()); // { done: false, value: 2 }console.log(iterator.next()); // { done: true, value: undefined }
Generators
A generator is a special type of function that works as a factory for iterators. A function becomes a generator if it contains one or more yield expressions and if it uses the function* syntax.
|
|
|
|
Örnek Kullanım
|
|