4 ms·
I prefer: for (let node of document.querySelectorAll("li")) node.classList.add('foo'); or: Array.from(document.querySelectorAll("li")).forEac
by megous 9y ago
I prefer:
for (let node of document.querySelectorAll("li"))
node.classList.add('foo');
or:
Array.from(document.querySelectorAll("li")).forEach(node => node.classList.add('foo'))
And if any particular pattern repeats in the code I'm writing I'll just create a helper function for that pattern (most of the time locally). Like:
let qsa => s => Array.from(document.querySelectorAll(s));
There's nothing to test about that.
- loganfsmyth 9y agoYou don't even need the `Array.from` anymore. `.forEach` on `NodeList` is standard https://developer.mozilla.org/en-US/docs/Web/API/NodeList/forEach https://developer.mozilla.org/en-US/docs/Web/API/NodeList/fo..., so all you need to do is load a polyfill for that.
- Flenser 9y agoYou might want to do map or filter before you do forEach. In which case you would need to convert it to an array.
- deleted 9y ago[deleted]