# IteratorPrototype

[JS](../..)⟩ [iteration](..) ⟩ [iterator]() ⟩ IteratorPrototype

{% tabs %}
{% tab title="💾 程式" %}
:floppy\_disk: [replit：IteratorPrototype](https://replit.com/@pegasusroe/IteratorPrototype#index.js)

```javascript
// ⭐ %IteratorPrototype%
// -------------------------------------------------------
//   prototype of all built-in iterators
//
//   const IteratorPrototype = {
//       [Symbol.iterator]() { return this },
//   };
//
// Note: 
//   in ECMAScript spec, internal objects are enclosed
//   in percent (%) signs.

// ⭐ %IteratorPrototype% is not directly accessible, 
//    but we can access it indirectly (from any built-in iterator).
//
// ⭐ array iterator -> ArrayIterator -> IteratorPrototype
const IteratorPrototype = Object.getPrototypeOf(    // IteratorPrototype
    Object.getPrototypeOf(                          // ArrayIterator
        [][Symbol.iterator]()    // ⭐ let empty array make an iterator
    )
);
```

💈範例：

```javascript
// what is the prototype of an array iterator?
let arrayIterator = [][Symbol.iterator]();
let proto = Object.getPrototypeOf(arrayIterator);
console.log(proto);              // Object [Array Iterator] {}

;[
    // `IteratorPrototype` is the prototype of ??? iterator ?
    'abc'[Symbol.iterator](),    // true (String iterator)
    [].keys(),                   // true (iterator of Array keys)
    new Map().entries(),         // true (iterator of Map entries)
    (function*() { })(),         // true (generator: iterable iterator)
    'aaa'.matchAll(/a/g),        // true (iterator of all results)

].forEach(iterator => {
    let ans = IteratorPrototype.isPrototypeOf(iterator);
    console.log(ans);
});
```
{% endtab %}

{% tab title="📗 參考" %}
* [ ] 2ality ⟩ [the prototype of all iterators in the standard library](https://2ality.com/2021/08/iteration-helpers.html#%25iteratorprototype%25%3A-the-prototype-of-all-iterators-in-the-standard-library)
{% endtab %}

{% tab title="👥 相關" %}
* [proto](../../val/obj/proto "mention") of an object.
{% endtab %}

{% tab title="📘 手冊" %}
* [String.prototype.matchAll()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/matchAll)
* [Object.prototype.isPrototypeOf()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isPrototypeOf)
{% endtab %}
{% endtabs %}
