Is it possible to define an array iterator that adjusts to your for-of syntax?

Logan Smyth loganfsmyth at gmail.com
Sun May 22 19:35:16 UTC 2016


It is not possible to detect this.

```
for (let [i, elem] of arr){
```

is no different that than

```
for (let pair of arr){
  let [i, elem] = pair;
```

You are destructuring the result of an iterator, and the initialization of
the iterator is independent from the initialization of the destructuring
pattern.



On Sun, May 22, 2016 at 10:55 AM, Šime Vidas <sime.vidas at gmail.com> wrote:

> Say I have an array over which I need to iterate multiple times, but I
> need the index value only some of the time. Is it possible to create a
> custom iterator which auto-detects when I need the index and feeds me
> entries() instead of values() in those cases? For example:
>
> array[Symbol.iterator] = /* some magic here */
>
> for (let elem of array) { /* values() iterator is used automatically */ }
> for (let [i, elem] of array) { /* entries() iterator is used automatically
> */ }
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20160522/6acbc802/attachment.html>


More information about the es-discuss mailing list