🔰generator function as ...

JSobjectsbuilt-inGeneratorgenerator function ⟩ as ...

// ⭐️ function* expressions

//           ╭─────── 🔸 1. as "variable" ───────╮
   const f = function*() { yield 'a'; yield 'b'; };
// ╰────────────── const statement ───────────────╯

let obj = {

    // (part of) keys
    x: 1, y: 2, z: 3,

    //╭─────────────── 🔸 2. as "method" ───────────────╮
    *keys() {
        for (let key of Object.keys(this)) { yield key }
    }
};

// 1. as variable
[...f()]            // [ 'a', 'b' ]

// 2. as method
[...obj.keys()]     // [ 'x', 'y', 'z', 'keys' ]

Last updated