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

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -