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

Šime Vidas sime.vidas at
Sun May 22 17:55:36 UTC 2016

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
*/ }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list