Removing Noise With Generators
Learn how to create and call generators with examples.
We'll cover the following...
A generator, as the name indicates, generates values.
📍 For a function to serve as a generator, its name should lead with a
*
and its body should have one or moreyield
calls.
Creating a generator
Let’s convert the iterator method to a simple generator.
Press + to interact
'use strict';class CardDeck {constructor() {this.suitShapes = ['Clubs', 'Diamonds', 'Hearts', 'Spaces'];}//START:METHOD*suits() {for(const color of this.suitShapes) {yield color;}}//END:METHOD}
-
We replaced the ...