๐Ÿ”ฐgenerator function as ...

JS โŸฉ objects โŸฉ built-in โŸฉ Generator โŸฉ generator function โŸฉ as ...

generator function can be used 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