3 ms·
I generally replace jQuery with something like: $ = (s, p = document) => p.querySelector(s) $$ = (s, p = document) => p.querySelectorAll(s) If I’m fee
by feupan 5y ago
I generally replace jQuery with something like:
$ = (s, p = document) => p.querySelector(s)
$$ = (s, p = document) => p.querySelectorAll(s)
If I’m feeling arrayish I’ll wrap the latter in [...qSA] so I can use .map and the rest of them, otherwise for/of works just as well.
Anyone using $.animate() and $.ajax() this year should probably stop.
- fabiospampinato 5y agoFor some basic use cases that's probably good enough, but saying that that can replace jQuery is like saying that farts can replace rocket engines. For example I can do the following with jQuery: $(document).on ( 'click.ns', '.button', callback ); How should one do the same with vanilla JS? You'd end up rewriting a (probably buggy) reimplementation of jQuery's $.fn.on method in the end.
- nomdep 5y agoYou use this library: https://www.npmjs.com/package/delegated-events https://www.npmjs.com/package/delegated-events
- fabiospampinato 5y agoIt doesn't support event namespacing.
- feupan 5y agoI haven’t used namespacing in years. The point people are trying to make is that if you do need “advanced jQuery features” maybe you shouldn’t use jQuery. And the basics are also covered by native API, so the same conclusion applies. I would not start a new project with jQuery nowadays just for that stuff. The fact that $(s).remove() never throws for example wouldn’t be ok for me anymore, so I’m looking at more cons than pros.
- earthboundkid 5y agoWith the code here https://news.ycombinator.com/item?id=29979680 https://news.ycombinator.com/item?id=29979680 it would be on("click", ".button", callback). It doesn't take much to replace most uses of jQuery.
- fabiospampinato 5y agoExcept that's broken beyond belief. Neither event delegation nor event namespacing are implemented in that snippet.
- earthboundkid 5y agoWeird definition of broken: not having features nobody asked for.
- fabiospampinato 5y agoHello, I explicitly asked for it: > How should one do the same with vanilla JS? And you said: > With the code here
- earthboundkid 5y agoI used jQuery back in the day and I never used namespaces, so I had no idea what ns.click meant. I don't really see why it's a desirable feature now. It just seems like a weird artifact of the fact that jQuery predates CustomEvents. I mean, I guess if there were no CustomEvents, it would be useful, but they exist now, so just use them.
- fabiospampinato 5y agoFor the record the code you linked to doesn't implement event delegation either, which is something pretty useful that even more modern libraries like React use under the hood. Here's [0] an article about the usefulness of namespaces, it has use cases beyond custom events, which have been implemented a real long time ago. [0]: https://css-tricks.com/namespaced-events-jquery/ https://css-tricks.com/namespaced-events-jquery/