I started writing JavaScript code because of WordPress back in 2007. JavaScript was not what it is today. It would behave differently on different ...
For further actions, you may consider blocking this person and/or reporting abuse
If you work on a distributed team or with legacy code, and considering the potential for conflict (mostly with jQuery but some other libraries, and any code where someone may have used
$
fordocument.getElementById()
, as used to be common), I think it'd be a good idea to use trivially longer identifiers like this:This is similar to a common method of using jQuery in noConflict mode, e.g.:
It could be judiciously extended for similar methods if you use them often enough.
I have been using
$ = function(e) {return document.getElementById(e)}
for years. Now I started using
$ = function(e) {
switch (e[0]) {
case '.': return document.getElementsByClassName(e.substring(1, e.length)); break;
case '#': return document.getElementById(e.substring(1, e.length)); break;
default: return document.getElementsByTagName(e);
}
}
Chrome has its own Console Utilities API Reference where
$
and$$
are offered regardless, among other utils, unless the global scope has been overwritten with something else (i.e. jQuery).The API for
$
and$$
is more jQuery-ish, and if you'd like to reproduce it its like:To avoid needing polyfills for
forEach
orSymbol.iterator
onNodeList
collections, you can also go ahead and convert the static collection asArray
so that all methods, includingfilter
andmap
can be used.If you don't have
Array.from
in your debug session, or you are after a thin layer that works across browsers, you can go for the following:Watch out that
querySelectorAll
returns aNodeList
object, whereas$$
in the console returns an array.So if you're used to call all the sweet array prototype methods, you might fall short!
Solution: wrap everything in
Array.from
.Thanks for reminding us :)
This is my take on it. It checks whether I'm asking for an id or not, and if I am, it returns just that one element. If not, the NodeList is made into an array.
I'm using
document.querySelectorAll
for all elements. Even for id elements.Actually, you can use
querySelectorAll
for id elements. It will work for one element. There must not be multiple elements in a document that have the same id value.Instead of using 2 different constants, what about checking the length of the selector first like this:
or something like that to make it much more similar to jQuery $?
From a performance perspective, that would double the amount of work for every selector. Going from jQuery to Vanilla JS is mostly about being more performant, so that looks like an anti-pattern.
Can you explain how would that be the double amount of work? It's just one extra call.
It's 2 calls instead of one, so it's double.
Ugh, I had to replace jQuery in our app with a work-a-like jQuery that I wrote. It was THE WORST, because I had to learn jQuery, then I learned all about jQuery's internals, then I had to figure out the subset of jQuery that we depended upon that did not have all the internal state that jQuery had (did everyone know jQuery has a ton of internal state?), then I had to put up with all the upset other JavaScript developers who complained about my not-jQuery not behaving like jQuery when they added new code that relied on jQuery-isms that I hadn't implemented because the prior versions of the code hadn't relied upon those facilities (so I didn't bother to implement them).
What a royal pain in the keester.
I wish I had the foresight to just do your $ and $$ bindings and tell all the other devs to suck it up. Your solution is GENIUS.
Could you explain the purpose of creating your jQuery-like library? I don't understand what you gained, compared to just continuing to use jQuery.
The lawyers at my company would not allow us to ship or use third party libraries.
I'm interested in your thoughts a little deeper on this:
"I advocate modern JavaScript. Because it's super awesome." - Why would you advocate modern JavaScript ahead of jQuery beyond it being awesome. What do you feel is the main selling point is that can't be accomplished in other ways?
This looks cool.
with you example could you still do this though?
Yep, it would look like this:
The outer
$
in the template string will start the evaluation. the$
inside of${}
will refer to the global$
.Hope this clears it up!
now that I think about it, I guess this would be a scope issue
π
This is an awesome tip!
Things like this are AMAZING so long as you don't have to support any legacy systems. It's nice to see that the feature set built by jQuery is essentially built into the browser directly now, and a simple wrapper gives us similar functionality.
I already did this in some projects that's cool !
But one point to take care is about performance.
Because querySelector do a lot more jobs thant getElementsById or class so if you dont care about perf you solution is perfect
Great tip Ahmad.
Also, in Chrome dev tools, $ and $$ are already bound by default to querySelector and querySelectorAll respectively.
Isn't
$
and$$
already binded toquerySelector
andquerySelectorAll
from the start though? Which means that binding it again seems unnecessary.or I suppose the default binding is recent..?
I made a module exactly for these purposes just recently: gimme back my dollars! It has a very distinctive name.
one question.
i always did it like this:
what are the differences?
this
Great tip Ahmad.
Thank you for sharing.
...and finally we returned to jQuery
cool :)