# 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 %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lochiwei.gitbook.io/web/js/iteration/iterator/iteratorprototype.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
