JQuery has some uses.

  • 原文: Why I’m Still Using jQuery in 2019
  • Translator: Fundebug

To ensure readability, free translation rather than literal translation is used in this paper. The translation is for study only and does not represent the views of Fundebug.

A lot of people are saying, “Just use native JavaScript, no jQuery”.

JQuery is trying to tell us how easy it is to get rid of jQuery. However, the first example shows that jQuery is not a bad idea, because we wrote 10 lines of native JavaScript code when we only needed one line of jQuery code.

Many JavaScript apis, especially DOM related apis, challenge my aesthetic philosophy and, frankly, I find them terrible! El.insertadjacentelement (‘afterend’, other) could also be used, but $(el).after(other) is more concise. The $() function isn’t that pretty either, I don’t particularly like it, but it’s way better than the native API.

How do you get the sibling of an element? Is it nextSibling or nextElementSibling? How are they different? Which method does each browser support? I’ll just use jQuery next and prev() when you’re busy looking up documentation in MDN.

Many of the standard JavaScript apis that are commonly used are quite strange. I won’t list them here, but You might not Need jQuery to find out.

You might not need to use jQuery to create help functions so that You don’t have to copy code on Stack OverFlow every time.

Browser compatibility isn’t as much of a headache as it used to be, but it’s still a big deal unless you want 85% of your users to be OK. For this, you can read my Why Hello CSS Doesn’t Use CSS variables.

So, do we have to use jQuery? Of course not! Using any third party library comes at a cost in terms of increased complexity and file size. However, jQuery isn’t really that big, it’s only 30K compressed. If we take away Ajax and some of the less common features, it’s only 23K; If we build with querySelector instead of SizzleJS, it’s only 17K. Whether 30K or 17K, this size is perfectly acceptable for many applications.

Take a look at Bootstrap Removing jQuery. They’re doing a lot of work removing jQuery. Abandon COMPATIBILITY with IE because it’s too hard to write; They spent a year and a half working on this. The end result seems to me not worth the effort.

I understand why they do this, people want to use Bootstrap in their Vue projects, but it’s silly to use both Vue and jQuery. I agree with The idea of reducing The size of Web pages, and I like The ideas in Web Bloat and The Ethics of Web Performance. But we need to be realistic and not too idealistic. Is adding 17K jQuery really that bad? When I joke that Medium or the New York Times require more than one Megabyte of JS files, some people retort: “Are you still using 56K bandwidth? But why is 17K jQuery so unacceptable?

It is also possible to write your own helper functions without using jQuery, if you want your functions to be reused by others, or if your functions are too small. But in order not to use jQuery, abandon backward compatibility? I think I’ll just use jQuery. Using jQuery for everything is certainly not a good idea, but using jQuery all the time is not a good idea either.

I’m not a big fan of jQuery, and I’m willing to use simplified versions of jQuery as long as they make the JS API a little more usable. You Might Not Need jQuery recommends Bonzo and $DOM, as well as a few other AJAX libraries, but most seem poorly maintained. It’s best not to replace jQuery without a compelling reason, because there’s nothing wrong with jQuery.

Some readers may compare Vue, React, and other front-end frameworks, but the purpose of this blog post is to compare jQuery with native JavaScript, not to argue how the whole front-end should be developed.

Having said that, I think there are scenarios where simple JavaScript is fine, mainly because I want the web page to be fast enough for as many people as possible to access. In my experience, using server-side rendering, coupled with progressively enhanced JavaScript, is the best way to implement it and is simple to develop, fast enough to access, and minimally buggy.

Isn’t the front end framework good? Of course not, there is no such thing as a bad technology, and all technologies have trade-offs, including jQuery.

reference

  • GitHub: Here’s how we abandoned jQuery
  • Why jQuery is obsolete?
  • Why are fewer and fewer people using jQuery

About Fundebug

Fundebug focuses on real-time BUG monitoring for JavaScript, wechat applets, wechat games, Alipay applets, React Native, Node.js and Java online applications. Since its launch on November 11, 2016, Fundebug has handled more than 1 billion error events in total, and paid customers include Google, 360, Kingsoft, Minming.com and many other brands. Welcome to try it for free!

Copyright statement

Reprint please indicate the author Fundebug and this article addresses: blog.fundebug.com/2019/06/04/…