noob here, can someone explain me like I'm five, what is so bad about jQuery? I use it constantly (mostly for creating tags for tag manager) and whenever I learn that there is no jQ on a page that I have to work on I get annoyed cause it means much more writing to get something a simple jQuery method would accomplish.
Think of jQuery as a millennium Falcon Lego box kit. Out of the box, it makes something pretty cool, but if your trying to make a little Lego dog, you've got a whole lot of pieces that never get used, but you have to carry them around anyway.
This is how vanilla JavaScript fans feel about jquery
You don’t need it... but I see that first example on JSON and the jquery is 1 line of code where the vanilla JS is 10 lines. It’s hard to be motivated to do the 10 line version when I can do the 1. It would be nice to see ES2015+ examples since we would use Babel to scale back to 9 anyway.
Of course the equivalent examples are dated, they're intended to work in old versions of IE so that they support the same versions as jQuery. You can't use fetch instead of jQuery if you need to support IE9.
Just use something like babel to compile your ES6 code. Jquery really isn't necessary anymore and honestly its better to just get more fluent with ES6 for your career anyways. None of the technical architects at my company are interested in new hires knowing Jquery anymore.
And out of your Millennium Falcon LEGO kit, we all know you only REALLY use the selector engine and click controls for the most part when your building your ship.
So we decided we’d rather build a faster millennium falcon from scratch without all the extra pieces we don’t need.
(ps. I don’t hate jquery. It had its time and place and made a lot of things better. We’ve moved on and these tools are no longer needed for their intended purpose. jQuery needs to be remembered fondly and forgotten.)
Development in general is moving towards more declarative than imperative frameworks and libraries.
With something like React you're describing what you want rendered, not how to render it. A component is only responsible for the bits it touches, and doesn't affect other parts of the page, and nothing else is going to come along and pull the rug out from under you.
With jQuery you're grabbing random bits of the page and mutating them without a cohesive rhyme or reason. You have to look over the entire code base to understand how the application works. Some other bit of code somewhere could change something out from underneath you and you'll never know about it till it breaks. This is how you end up with spaghetti code.
jQuery was useful in it's day as a nice abstraction over an ugly DOM API, and can be suitable for tiny minor jobs in a single page, but is not fit for purpose for architecting a full application.
I don't think jQuery is necessarily bad, it's just becoming harder to justify its existence.
Writing any JavaScript was a nightmare back when we had IE6 and IE7 in regular use alongside early versions of Chrome and Firefox since IE would just ignore so many standards; your site would be a work of art in Firefox but would look like a hot mess in IE. jQuery (and prototype, mootools etc) made it so you could write code once and be fairly sure it would work across all browsers. It also added some really useful ways of accessing DOM elements, looping over arrays, manipulating CSS on the fly
These days IE is almost dead, most browsers tend to behave themselves and ES6 is adding a lot of the functionality I used to use jQuery for. In fact, the only reason I need to include it these days I because I use bootstrap on most projects.
Keeps laughing in monolithic organisation since XP still work and noone will ever ask for new OS/application development even if they have to install Citrix that will share IE, since it's actually cheaper
Source: have seen some crazy stuff in both public and private sector
I was thinking about adding event listeners (.on) or easily iterating over some content (.each) , but I checked your link and compared it and you're right. Thanks for the site it will come in handy!
It's a bit out of date in that if you are able to use ES6 features like for..of loops, things get quite a bit nicer than the examples on that site. And I haven't been asked to support IE10 in a few years now, let alone 9 and 8.
You mentioned "on". One thing to be aware of which is not clear in the addEventListener example is that while jQuery will let you set listeners for all elements in its list at once, you'd need to loop over the elements (this is one example of where for..of is really nice) and run an addEventListener on each. Similarly event delegation, which "on" makes easy, is a little more fiddly without jQuery. It's not difficult, just not quite so terse.
Off the top of my head (and I'm on mobile) to set listeners on a bunch of elements (this covers both "on" and "each"):
for (const a of document.querySelectorAll("a.foo")) {
a.addEventListener("click", handlerFunction);
}
To do it with event delegation:
document.getElementById("my-container").addEventListener("click", (event) => {
const a = event.target.closest("a.foo");
if (a == null) return;
// Do something with a, the clicked anchor element
});
Note that depending on target browsers you might need polyfills, such as for Element.closest.
I find jQuery as tool for lazy people(I'm using jQuery everywhere where I can myself).
It's easier to do things in jQuery, but there are loading time cost for it. But we could just do same tasks in plain javascript with paying more effort to it.
72
u/Grrrucha Feb 13 '19
noob here, can someone explain me like I'm five, what is so bad about jQuery? I use it constantly (mostly for creating tags for tag manager) and whenever I learn that there is no jQ on a page that I have to work on I get annoyed cause it means much more writing to get something a simple jQuery method would accomplish.