ecmascript 6 - ES6 for of - is iterable evaluated only once? -


consider simple of:

for (const elem of document.getelementsbytagname('*') {   // elem } 

does getelementsbytagname evaluated once or on each iteration ?

thx!

in case, it's evaluated once obtain iterable, uses obtain iterator. reuses iterator grab values , pass them for block. it's similar doing following generator function:

function* getintegers(max) {   (let = 0; <= max; i++) {     yield i;   } }  const iterator = getintegers(15);  while (true) {   const { done, value } = iterator.next();   if (done) {     break;   }    console.log(value); } 

as noted loganfsmyth, generator functions return iterator directly. note: generator functions can used for..of construct.

see this article on mdn more info.


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -