Another option is to attach the on method plus Array.prototype.map to the NodeList prototype instead:
const$$=(selector,startNode=D)=>startNode.querySelectorAll(selector)NodeList.prototype.map=Array.prototype.mapNodeList.prototype.on=function(type,listener,options){this.map(el=>{if(elinstanceofElement){el.addEventListener(type,listener,options)}})returnthis// for chaining}
Found it while researching our story on vanilla JS. Also been working on an open source list of paid & free quality resources for devs to learn vanilla JS--open for PRs. Maybe I should add an "article" section with content such as yours?
The reason I did the check is that NodeList gets converted into an Array via the $$ function. NodeLists are read-only and always contain Element elements, so it is safe to call addEventListener on them without the check. Arrays are not read-only and the array elements are not safe from reassignment.
Everything's nice now, you need to work off-line ?
var offdoc = doc.cloneNode()
// mind that some browsers don't create offdoc.documentElement so
if (!offdoc.documentElement) offdoc.appendChild(offdoc.createElement('html'))
offdoc.documentElement.innerHTML = '<head></head><body></body>'
// this won't load the file
offdoc.head.insertAdjacentHTML('beforeEnd', '<script src="toto.js"></script>')
// and this still works
offdoc.$('script')
In this case, another option is to define another shortcut for documents other than document. For example, you can move the $ function definition inside a function that takes a document as a parameter:
Yes, Bliss.js by Lea Verou also provide the $ and $$ shortcuts for querySelector and querySelectorAll, plus more functions that make it a fully-fledged, powerful and lightweight framework.
I teach college web development classes in Denmark and I have been messing around on the Internet since the WWW became a thing.
I love simple solutions and humour!
Great article!
I am not sure how I feel about altering built-in prototypes, I generally like to keep those clean. But the jquery-like syntax and the ability to chain event listeners without having to actually use jQuery is super sweet! 👌
Web Developer currently experienced in JavaScript, React, Node.js but interested to learn multiple technologies and not stick to dead ones.
Specialization- Building high performance SPAs.
Instead of
startNode||DI would recommend default arguments – it's exactly the reason they were added to the languageGood point :)
Another option is to attach the
onmethod plusArray.prototype.mapto theNodeListprototype instead:This keeps the Array prototype clean =).
Short but sweet post, Lea! 🙌
Found it while researching our story on vanilla JS. Also been working on an open source list of paid & free quality resources for devs to learn vanilla JS--open for PRs. Maybe I should add an "article" section with content such as yours?
Hi Franck, thank you! I love your list of resources for Vanilla JS. Feel free to add it as you like :)
I like that you monkey-patch
Array#on- but is there ever an instance that aNodeListreturned fromquerySelectorwouldn't return anElement?What I'm trying to ask is:
Is
if (el instanceof Element)a necessary check?The reason I did the check is that
NodeListgets converted into anArrayvia the$$function.NodeLists are read-only and always containElementelements, so it is safe to calladdEventListeneron them without the check.Arrays are not read-only and the array elements are not safe from reassignment.Hi, ignoring with which document you're working is a quick way to bang your head to the wall. Always keep a reference to which document you're in:
A shorthand for document, why not:
Everything's nice now, you need to work off-line ?
In this case, another option is to define another shortcut for documents other than
document. For example, you can move the $ function definition inside a function that takes adocumentas a parameter:Great tips
Another Lea happens to have made something similar: blissfuljs.com/
Yes, Bliss.js by Lea Verou also provide the
$and$$shortcuts forquerySelectorandquerySelectorAll, plus more functions that make it a fully-fledged, powerful and lightweight framework.Love it!
Definitely going to incorporate this in my snippet library.
Great article!
I am not sure how I feel about altering built-in prototypes, I generally like to keep those clean. But the jquery-like syntax and the ability to chain event listeners without having to actually use jQuery is super sweet! 👌
We should avoid mutating the Array prototype though. That's why MooTools SmooshGate happened. Good post though, short and simple