Skip to content Skip to sidebar Skip to footer

Edge 15 Throws Error When Using 'for ... Of' On A Nodelist

When looking at the ECMAScript compatibility table, it says that Edge 15 and Edge 16 support for ... of loops. However, when I run this code: It works in Chrome and Firefox, but n

Solution 1:

Edge does support for... of.

It would seem that it doesn't support iterators on NodeLists. Not all array-like objects support iterators and I'm not sure whether there is any standard saying that NodeLists have to.

In any case, it's easy enough to get for ... of to work with them:

const list = document.querySelectorAll('[data-test]');

for (const item ofArray.from(list)) {
	console.log(item);
}
<divdata-test>a</div><divdata-test>b</div><divdata-test>c</div><divdata-test>d</div><divdata-test>e</div>

Solution 2:

If for..of is supported but Edge 15 forgot to add the behavior to NodeList, you could polyfill it yourself with very little code:

NodeList.prototype[Symbol.iterator] = function* () {
    for(var i = 0; i < this.length ; i++) {
        yieldthis[i]
    }
}

To answer the other question (is it defined as iterable in the spec?) the answer is yes:

The DOM spec defines NodeList as:

interface NodeList {
  getter Node? item(unsignedlong index);
  readonly attribute unsignedlong length;
  iterable<Node>;
};

Note the third property, iterable<Node>;. Looking it up in the WebIDL spec:

In the ECMAScript language binding, an interface that is iterable will have “entries”, “forEach”, “keys”, “values” and @@iterator properties on its interface prototype object.

It seems that Edge doesn't implement any of that.

Post a Comment for "Edge 15 Throws Error When Using 'for ... Of' On A Nodelist"