> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/web/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/web/js/iteration/generator/func/as.md).

# generator function as ...

[JS](/web/js.md) ⟩ [objects](/web/js/val/obj.md) ⟩ [built-in](/web/js/val/builtin.md) ⟩ [Generator](/web/js/iteration/generator.md) ⟩ [generator function](/web/js/iteration/generator/func.md) ⟩ as ...

{% hint style="success" %}
[generator function](/web/js/iteration/generator/func.md) can be used as：

* [variable](/web/js/variable.md), [method](/web/js/val/obj/method.md)
  {% endhint %}

{% tabs %}
{% tab title="💈範例" %}

* replit：[generator functions as ...](https://replit.com/@pegasusroe/generator-function-as-method#index.js)

```javascript
// ⭐️ 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' ]
```

{% endtab %}

{% tab title="📗 參考" %}

* [ ] JavaScript: The Definitive Guide ⟩ 12.3 Generators
  {% endtab %}
  {% endtabs %}
