Hacker News new | ask | show | jobs
by troupo 17 days ago
No, you're not.

jQuery returned useful collections. DOM methods return an anemic NodeList which doesn't even implement Array interface.

3 comments

NodeList has .values() that returns an iterator.

    var $$ = q => document.querySelectorall(q).values()
Then you have access to a bunch of iterator methods

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

That's why I do

  $ = (selector) => Array.from(document.querySelectorAll(selector))
Still not as good as the original, but somewhat usable at least :/
There are small libraries like umbrellajs that just provide a jquery-like DOM interface if that's all you want.

  $('body').childNodes.forEach(console.log)
I see a beautiful collection there
NodeList exposes exactly 5 methods: item, entries, forEach, keys, and values.

Very beautiful.

BTW, browsers didn't even add forEach to it until several years after they added document.querySelectorAll lol

Edit. https://caniuse.com/?search=nodelist

NodeList appeared in 2006(Firefox) - 2008 (Safari) - 2010 (Chrome)

forEach on it appeared in... 2016.