← Back to context

Comment by jraph

17 hours ago

Doesn't querySelector(All) provide this?

Not entirely. There's a reason people do Array.from(querySelectorAll) to do more than just `forEach`

  • So you are saying that Array.from(querySelectorAll) gets you there? What are you missing then?

    Genuinely asking, I have no clue what's being alluded to without being clearly mentioned in this thread.

    • > So you are saying that Array.from(querySelectorAll) gets you there? What are you missing then?

      Array.from adds friction. The need to wrap querySelector in null checks adds friction. The fact that they are not composable in any way, shape, or form, with any DOM methods (and that DOM methods are not composable) adds friction.

      jQuery was the fore-runner of fluid interface design. Nothing in the DOM before, then, or since ever thought about it. Even the new APIs are all the same 90s Java-style method calls with awkward conversions and workarounds to do anything useful.

      That's why sites like "You don't need jQuery" read like bad parody: https://youmightnotneedjquery.com

      E.g. what happens when it's not just one element?

         $(el).addClass(className);
      
         // vs.
      
         el.classList.add(className);
      
      

      Or: why doesn't NodeList expose an array-like object, but provides an extremely anaemic interface that you always need to convert to array? [1]

         $(selector).filter(filterFn);
      
         // vs.
      
         [...document.querySelectorAll(selector)].filter(filterFn);
      
      

      There's a reason most people avoid DOM APIs like the plague.

      ---

      [1] This is the entirety of methods exposed on NodeList https://developer.mozilla.org/en-US/docs/Web/API/NodeList

      Instance properties

      - length

      Instance methods

      - entries() // returns an iterator

      - forEach()

      - item()

      - keys()

      - values()

      8 replies →