# make iterables

[JS](https://lochiwei.gitbook.io/web/js)⟩ [iteration](https://lochiwei.gitbook.io/web/js/iteration) ⟩ [iterable](https://lochiwei.gitbook.io/web/js/iteration/iterable) ⟩ make iterables

{% tabs %}
{% tab title="⭐️ 重點" %}
{% hint style="success" %}
we can make [class](https://lochiwei.gitbook.io/web/js/val/class "mention")/[obj](https://lochiwei.gitbook.io/web/js/val/obj "mention") [iterable](https://lochiwei.gitbook.io/web/js/iteration/iterable) by letting them：

* make [iterator](https://lochiwei.gitbook.io/web/js/iteration/iterator "mention") <mark style="color:yellow;">**object**</mark> directly ( 👉 see： [closedrange](https://lochiwei.gitbook.io/web/js/iteration/iterable/custom/closedrange "mention"))
* return new [iterator](https://lochiwei.gitbook.io/web/js/iteration/iterator "mention") using [func](https://lochiwei.gitbook.io/web/js/iteration/generator/func "mention") ( 👉 see：�[�例一](#fan-li) )
  {% endhint %}
  {% endtab %}

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

* [ ] 2ality ⟩ [iterable iterator](https://2ality.com/2021/08/iteration-helpers.html#iterators-that-are-also-iterable)
  {% endtab %}
  {% endtabs %}

## examples

{% tabs %}
{% tab title="💈例一" %}
{% hint style="success" %}
use [func](https://lochiwei.gitbook.io/web/js/iteration/generator/func "mention") to return new [iterator](https://lochiwei.gitbook.io/web/js/iteration/iterator "mention").
{% endhint %}

:floppy\_disk: replit：[make class iterable](https://replit.com/@pegasusroe/generate-iterables#index.js)

```javascript
// an iterable (that can make iterators)
class ValueContainer {

    // private properties
    #values;

    // init
    constructor(...values) {
        this.#values = values;
    }

    // ⭐ "make iterator" method
    // ⭐ use generator function to return new iterator
    *[Symbol.iterator]() {
        for (const value of this.#values) {
            yield value;
        }
    }

}

// main
let container = new ValueContainer(1,2,3);
[...container];        // [ 1, 2, 3 ]
```

{% endtab %}

{% tab title="👥 相關" %}

* [private](https://lochiwei.gitbook.io/web/js/val/class/member/private "mention")
  {% endtab %}
  {% endtabs %}
