jskatas.org Continuously Learn JavaScript. Your Way.

Generator: creation

There are many ways to create a generator

Generators can be created in multiple ways

the most common way is by adding * after function
function g() {} assert.equal(String(g()), '[object Generator]');
as a function expression, by adding a * after function
let g = function() {}; assert.equal(String(g()), '[object Generator]');
inside an object by prefixing the function name with *
let obj = { g() {} }; assert.equal(String(obj.g()), '[object Generator]');
computed generator names, are just prefixed with a *
const generatorName = 'g'; let obj = { [generatorName]() {} }; assert.equal(String(obj.g()), '[object Generator]');
inside a class the same way
const generatorName = 'g'; class Klazz { [generatorName]() {} } assert.equal(String(new Klazz().g()), '[object Generator]');

Links

Describes the `function*` declaration.

Required Knowledge

Related Katas

Generator

Difficulty Level

TBD

First Published

1 June 2015

Stats

5 tests to solve