InterviewHubPRO
Section Overview
Q423

Question 423 of 521

Part 7: Execution Context, Functional JS & V8 Engine (404 - 463)

81%
JavaScript Internals (521 Qs)•Part 7: Execution Context, Functional JS & V8 Engine (404 - 463)

What are the different kinds of generators

There are five kinds of generators,

  1. Generator function declaration:
javascript
    function* myGenFunc() {
      yield 1;
      yield 2;
      yield 3;
    }
    const genObj = myGenFunc();
  1. Generator function expressions:
javascript
    const myGenFunc = function* () {
      yield 1;
      yield 2;
      yield 3;
    };
    const genObj = myGenFunc();
  1. Generator method definitions in object literals:
javascript
    const myObj = {
      *myGeneratorMethod() {
        yield 1;
        yield 2;
        yield 3;
      },
    };
    const genObj = myObj.myGeneratorMethod();
  1. Generator method definitions in class:
javascript
    class MyClass {
      *myGeneratorMethod() {
        yield 1;
        yield 2;
        yield 3;
      }
    }
    const myObject = new MyClass();
    const genObj = myObject.myGeneratorMethod();
  1. Generator as a computed property:
javascript
    const SomeObj = {
      *[Symbol.iterator]() {
        yield 1;
        yield 2;
        yield 3;
      },
    };

    console.log(Array.from(SomeObj)); // [ 1, 2, 3 ]