logoalt Hacker News

troupolast Friday at 6:17 PM1 replyview on HN

> including NodeList directly, I believe,

I listed all public methods and properties of NodeList. It does have a forEach, so there's not much need for `for of`

As for iterator methods, I completely forgot about that :) Yeah, you can/will be able to use them on .entries()


Replies

WorldMakerlast Friday at 7:07 PM

You missed [Symbol.iterator] as a public method. I prefer the aesthetics of for/of over forEach in most cases, but it's as much personal preference as anything.

I did briefly forget the distinction between Iterable (has [Symbol.iterator]) and Iterator (the thing [Symbol.iterator]() returns). You can use the Iterator helpers "directly" on the NodeList with `someNodeList[Symbol.iterator]().map(…)` or `Iterator.from(someNodeList).map(…)`. There are advantages to that over `Array.from` but not many advantages over `someNodeList.entries().map(…)`.

(I partly forgot because I assumed MDN hadn't been updated with the new Iterator helpers, but of course it has [1], they are marked as "Baseline March 2025" [all browsers updated since March 2025 support them] and there is a lot of green in the browser compatibility tables. caniuse suggests Iterator.prototype.map is ~84% globally available.)

[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

show 1 reply