> 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/iterator/iteratorprototype.md).

# IteratorPrototype

[JS](/web/js.md)⟩ [iteration](/web/js/iteration.md) ⟩ [iterator](/web/js/iteration/iterator.md) ⟩ 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="👥 相關" %}

* [prototype](/web/js/val/obj/proto.md) 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 %}
