DOM4 makes NodeLists iterable:
interface NodeList {
getter Node? item(unsigned long index);
readonly attribute unsigned long length;
iterable<Node>;
};
According to WebIDL, this means
Objects implementing an interface that is declared to be iterable support being iterated over to obtain a sequence of values.
Note: In the ECMAScript language binding, an interface that is iterable will have “entries”, “forEach”, “keys”, “values” and @@iterator properties on its interface prototype object.
So the following is possible:
for (var el of document.querySelectorAll(selector)) ...
I noticed the same seems to work for HTMLCollections, both on Firefox and Chrome:
for (var el of document.getElementsByTagName(tag)) ...
In fact, I get
HTMLCollection.prototype[Symbol.iterator] === [][Symbol.iterator]; // true
However, HTMLCollection is not defined as iterable:
interface HTMLCollection {
readonly attribute unsigned long length;
getter Element? item(unsigned long index);
getter Element? namedItem(DOMString name);
};
I also checked the WHATWG DOM spec and it's not iterable neither.
Then, is this behavior standard or not? Is HTMLCollection
supposed to have an @@iterator
in the prototype?