2 ms·
querySelectorAll() isn't live. So you could do what I very often do and already convert the result to an array, i.e. dqsA = s => Array.from(document.querySel
by nedt 2y ago
querySelectorAll() isn't live. So you could do what I very often do and already convert the result to an array, i.e.
dqsA = s => Array.from(document.querySelectorAll(s));
Reason why I do that very often is because it allows all array methods to be used on the result, like .map() or .filter(), which makes it feel very much like jQuery. YMMV
- theandrewbailey 2y agoNodeList.prototype.__proto__ = Array.prototype; Problem solved without Array.from().
- cr125rider 2y agoDoes the underlying data structure work okay with that? I would assume there is some sort of lazy iterator involved that may not work with array methods, or only work once. This is JavaScript though…
- jazzypants 2y agoPrototype pollution is bad. We learned this over a decade ago.
- mg 2y agoGood point! I wonder what I would have to look for in my codebase in terms of what could break when dqsA starts returning an array instead of a NodeList?
- WorldMaker 2y agoThe versions of those methods (map/filter/reduce/etc) that support any iterator (including upgrading NodeList "for free") have passed Stage 4 of the process, which means they will be in the next version of the standard and already starting to show up in some browsers. https://github.com/tc39/proposal-iterator-helpers https://github.com/tc39/proposal-iterator-helpers