The generator method

To treat a concise method of an object literal as the generator method, or to treat a method of a class as the generator method, we can simply prefix it with the * character.

The generator method of a class is added to the prototype property of the class.

Here is an example to demonstrate how to define a generator method in the class:

class myClass {      * generator_function()  {          yield 1;          yield 2;          yield 3;          yield 4;          yield 5;      } } const obj = new myClass(); let generator = obj.generator_function(); console.log(generator.next().value); console.log(generator.next().value); console.log(generator.next().value); console.log(generator.next().value); console.log(generator.next().value); console.log(generator.next().done); console.log("generator_function" ...

Get Learn ECMAScript - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.