Hacker News new | ask | show | jobs
by enumjorge 1333 days ago
If the argument is for minimalism, why use jQuery when vanilla JS suffices?
3 comments

What does VanillaJS get you over jQuery? The only practical argument I can think of is the performance improvement of not loading another library and loading all the extra KBs.

But nearly every Shopify website’s hero image itself would completely dwarf any bandwidth concerns jQuery might cause.

In the meanwhile, jQuery is a much better known API among developers, there’s far more and higher quality documentation and snippets available in it on the internet, and there are fewer foot guns.

jQuery easily seems the superior choice over VanillaJS, with very few downsides given Shopify’s use case.

because JQ is already loaded, and it IS minimal. You could do querySelector(...).addEventListener etc but why would you?
I get the purist push for Vanilla JS but for me it's still way too verbose. W3C has had decades to fix it and while it's improved it's still verbose as heck. Just implement the jQuery API into Vanilla JS and people would stop using jQuery.
That's what I did in Sciter:

1. Added element.$("selector") and element.$$("selector") functions. Later one allows to work with JQ sets:

     for(let el of parent.$$(".child"))
        ...
2. Added element.on("eventname" [,"selector"], handler) and element.off()

These two allow to reduce need for JQuery to almost zero.

Also added JSX as a built-in feature to JS/runtime. So,

     function Child(props) {
        return <p>Generated child {props.index}</p>;
     }

     function Main() {
        const list = [1,2,3];
        return <main>
          { list.map( el => <Child index={el} /> ) }
        </main>
     } 

     // add the list to the DOM:
     document.body.append(<Main />); 
These three simple things, together with elment.patch(...JSX...) eliminate need for as JQuery as ReactJS almost completely.
I'm really surprised they don't get more shit for it.

People love to make fun of Java for being verbose but then go all googoo eyes over Vanilla JS. As a Java developer for 20 years now, using it makes me think a Java developer from the '90s designed it.

But, you know, it's JS so it gets a pass.

"just"
Look at who makes up W3C. Then look at how much it costs to be a member.

They could afford anything if they cared to. Most of the time they don't and so the small players like Chrome get away with anything.

I mean the W3C has an annual revenue of $5.7M so it's not like they don't have the resources to start setting some vision and goals towards this.
Because you don't need another library to do it? Why load jQuery at all in 2022?
It's legacy. A lot of people know jquery from when the things it does were verbose in vanilla JS. New devs rarely learn jquery, so it will die.
A significant chunk of Shopify’s user base is gonna be copying/pasting snippets they’ve found online.

You’re much more likely to find a snippet using jQuery that was created over the last 10 years or so that’s consistent and works correctly than you are vanillaJS.

VanillaJS queries would be all over the place with multiple if/else’s for IE, Chrome, WebKit, Mozilla, etc.