> If you're developing a library on the other hand, please take a moment to consider if you actually need jQuery as a dependency.
Maybe I am mistaken but it seems to me that this site targets library developers who use jQuery without realizing that they're forcing it on users as a transitive dependency. It does not seem to apply to end-users writing simple tools.
With abstraction there has to be a balance though. Sure, I concede that knowing how it works is actually important, hence why I've spent a lot of time doing things without libraries in the past, especially as I was learning. It actually enrages me when people who've only ever used ORMs make super basic mistakes when writing and executing SQL themselves for the first time, i.e selecting a bunch of relationships in a loop one by one, instead of getting them all in one query. Stuff like that is a good example of how only ever working with abstracted versions of your tech stack results in mistakes down the line that have costs, since the abstracted version doesn't explain these concepts to you.
However, when it comes to actually trying to get things done efficiently, abstraction can help a bunch. Writing a bunch of stuff from scratch every time is not particularly productive either.
If you were to describe that in offline terms applications would be like books and abstractions would be like parts of the books. In order to make effective use of the book you still have to read, from scratch (whatever that means). When you become well versed in reading and writing it doesn't feel like a chore and you become far more efficient at it than simply guessing at the material from selected paraphrases. Programming is no different.
The point I was making is that we could all spend our time going as deep as possible with as little abstraction as possible and end up writing stuff in assembly because "it's important to know how something works".
There is always a cost/benefit with abstraction when it comes to getting things done.
I'm just curious :)
One of the first things I learned in web development was jquery but right now I never use it. Even if I create a vanilla html/css/js website.
It requires craft to get addEventListener() on par with on().
jQuery comes with many implicits loops that save you time.
fadeIn() is easier to handle than the equivalent css. jQuery supports edge if you target it (things like prepend(), parent() work on it).
Error handling is just better. E.G: jQuery().val() will not raise an error if there is no match while document.getElementById().value will.
And probably a hundred of other little details that won't come out of my head but that will grind my gear if I start working on code.
Chaining is awesome to manipulate DOM. The imperative API is very verbose.
So. Many. Plugins.
I'm more of a VueJS guy now, but if I have only a quick page to setup, the 30ko of jquery are better than anything I can produce to wrap my repetitive code.
The fetch API is more powerful than the Jquery one.
Since forEach is implemented in es6 I just use that.
I see the point that you need it sometimes but still I would prefer some other lighter solutions.
* Perhaps those 10 lines execute faster
* Perhaps those 10 lines are exactly 10 lines, where jQuery is 2 lines plus a 65k library
* Perhaps those 10 lines work equally in multiple environments (node, deno, electron, browser) where jQuery does not.
* Perhaps those 10 lines sit behind a custom abstraction that actually looks like a single method
* Perhaps those 10 lines do something jQuery does not
* Perhaps those 10 lines scale and extend in ways jQuery does not
* Perhaps those 10 lines have a desired side effect
Heh, and here we come full circle. I remember back when the whole point of jQuery was to handle the different underlying JS implementations for you.
ES6+ does not replace browser APIs. There's no ES6+ way around this:
const el = document.createElement(...)
el.setAttribute(...)
el.classList.toggle(...)
const parent = document.getElementById(...)
parent.appendChild(el)
You either do that in one line of jQuery, or end up writing your own wrappers if you need to do this more than once.It's more explicit—that will almost always win with me.
Once you write it not once, but twice, or five times, you will either switch to a lib/framework, or to jQuery, or will write your own wrapper not that different from jQuery.
insertElement(type: string, text?: string. attributes?: object, parent?: string): void;
I prefer writing something like that with some minor case-handling over pulling in a library every time I meet a repeatable fragment.If I know I'm going to run into a large host of needs, then it's a different story. But most of the time I find jQuery overkill and somewhat opaque.
I happily write them myself. It's a few seconds of work, really.
[1] Okay, there are some people, they're rare and generally regarded as weird.
Explicit [within the context of the language]. And by that I did mean human readable.
const divElement = document.createElement("div");
divElement.textContent = "Hello world";
document.appendChild(divElement);
Is more human readable to me than the jQuery abstraction (that pulls in piles of other potentially unused tooling) than: $(document).append("div").text("Hello, world");
It's more verbose, sure. But like I said in another comment, if I have to repeat the methods more than twice I'd probably just wrap the couple of lines into a function, like: function createTextElement(type: string, text: string): void {
const element = document.createElement(type);
element.textContent = text;
document.appendChild(element);
}
And anywhere that's called it's quite clear what is being done createTextElement("div", "Hello world");
This seems to be preferred when writing C as well, no? Rather than abstracting common methods to more opaque symbols?Maybe it's just me, but I prefer the English, descriptive version and I prefer working with code formatted the same way. The language (JS) has plenty of quirks as it is.
Comparing C/Assembly I don't think is a 1:1 fair comparison, though. Unless you're including TypeScript—which is how I tend to write JS anyway (whenever possible).
I wrote in a sibling comment:
Once you write it not once, but twice, or five times, you will either switch to a lib/framework, or to jQuery, or will write your own wrapper not that different from jQuery.
> Error code: SSL_ERROR_BAD_CERT_DOMAIN
trying to access https://youmightneedjquery.com .
Array.prototype.filter.call(document.querySelectorAll(selector), filterFn);
What's more idiomatic is document.querySelectorAll(selector).filter(filterFn)
The same holds for forEach.
I suggest you look into how javascript prototype based OOP works
This is why people convert it to an array, and one of many reasons the standard JS API (and the DOM one in particular) are annoying to work with.
[...document.querySelectorAll(selector)].filter(filterFn);If anything isn't that code _less_ idiomatic in that it's less specific to JS and more of a generic operation?
If you've seen that more common than call/apply for years then you probably work almost exclusively on new projects, with people that convert things to bleeding-edge, or with heavy transpilation: The spread operator as used there has only existed in regular released browsers/node for 3-4 years. I don't think it's a stretch to say the vast majority of code out there at this time won't be doing it that way.
Definitely colours my work, and knowledge base.
Pretty quickly I found myself wishing I had just used jQuery or at least some other library to make stuff easier and get things done faster. When I finally got it completed in the end I wasn't sure why I bothered to struggle and waste so much time instead of just including jQuery and making things a bunch easier and less verbose. Sure, there may be other libraries worth using instead but the point against the "vanilla JS is all you need" argument.
I actually found the cited website in this post http://youmightnotneedjquery.com to be so strange as examples of why you may not need jQuery that it may be satire. I was surprised to find out that it was not satire. So I actually forked it and created https://youmightneedjquery.com, on which I may change some text in the future. It may not load for you yet as the DNS propagates.
You can find the fork on my Github account here: https://github.com/JaTochNietDan/youmightnotneedjquery
I'll make some changes later to show the satirical nature of it.