javascript - Are HTMLCollection and NodeList iterables? -
in es6, iterable object allows for... of
, , has symbol.iterator key.
arrays iterables, sets , maps. question is: htmlcollection , nodelist iterables? supposed be?
mdn documentation seems suggest nodelist
iterable.
for...of
loops loop on nodelist objects correctly, in browsers supportfor...of
(like firefox 13 , later)
this appears corroborate firefox's behaviour.
i tested following code in both chrome , firefox, , surprised find firefox seem think iterables, chrome not. in addition, firefox thinks iterators returned htmlcollection
, nodelist
1 , same.
var col = document.getelementsbyclassname('test'); // should htmlcollection of 2 elems var nod = document.queryselectorall('.test'); // should nodelist of 2 elems var arr = [].slice.call(col); // should array of 2 elems console.log(col[symbol.iterator]); // firefox: iterator function, chrome: undefined console.log(nod[symbol.iterator]); // firefox: iterator function, chrome: undefined console.log(arr[symbol.iterator]); // firefox & chrome: iterator function console.log(col[symbol.iterator] === nod[symbol.iterator]); // firefox: true console.log(col[symbol.iterator] === arr[symbol.iterator]); // firefox: false
<div class="test">1</div> <div class="test">2</div>
one weird, confusing thing: running code snippet produces different result copying , running in actual file/console in firefox (particularly last comparison). enlightenment on weird behaviour here appreciated too.
symbol.iterator
support nodelist
, htmlcollection
, domtokenlist
, , domsettabletokenlist
discussed , added whatwg's dom spec last year.
Comments
Post a Comment