ECMAScript Generator Basics
Generator in ECMAScript 2015 – Part 1
Foreword: In this part of the series, I give you the basics of ECMAScript generator.
By: Chrysanthus Date Published: 21 Jul 2016
Imagine a class of 5 students who wrote a test. 3 students had marks; one student was absent and the result of one was cancelled. The marks (result) may be written as follows:
This is a collection.
Creating a Custom Collection
You can create a custom collection in two ways. You can create the above collection like,
yield* [56, 70, 'absent', 60, 'cancelled'];
Note the use of the asterisk. myCollection is a name of your choice, while function is a reserved word. yield is also a reserved word.
Iterating through the Collection
The following for-loop iterates through the collection:
for (item of myCollection())
Note the use of “item of” where item is a variable name of your choice.
That is it for this part of the series.
Related LinksECMAScript Basics
Expressions in ECMAScript
Statements in ECMAScript
Custom Objects in ECMAScript
Functions in ECMAScript
ECMAScript Date Object
The ECMAScript String Object
ECMAScript String Regular Expressions
ECMAScript Template Literal
The ECMAScript Array
ECMAScript Sets and Maps
Scopes in ECMAScript
Sending Email with ECMAScript
ECMAScript Insecurities and Prevention
Advanced ECMAScript Regular Expressions
Promise in ECMAScript 2015
Generator in ECMAScript 2015
More Related Links
Node.js Web Development Course
Major in Website Design
Low Level Programming - Writing ECMAScript Module