3 ms·
The DOM API remains complete garbage unless you're operating readonly on a single node (then it's merely bad).
by masklinn 12y ago
The DOM API remains complete garbage unless you're operating readonly on a single node (then it's merely bad).
- zodiakzz 12y agoDo you have any examples? In my opinion the DOM API only sucked pre-IE9 because of all the inconsistencies and incompatibilities. I have dropped jQuery completely except for when I need some of its plugins because I end up having to access the internal DOM objects too often and using $el.get(0) all over the place is ugly and a PITA.
- vbezhenar 12y ago$('.something').toggleClass('collapsed') .click(somethingClicked); This simple and readable statement will turn into a lot of lines with DOM API.
- deleted 12y ago[deleted]
- thedufer 12y ago> I would argue that the second half is a little unfair, since that style of coding is entirely influenced by jQuery. You usually don't actually need to trigger a real DOM event there, you just want to execute some callback as if the element was clicked. Maybe I'm missing something, but the second half is adding an event handler (`somethingClicked` is presumably a function). That seems like a _very_ common thing to do regardless of jQuery.
- deleted 12y ago[deleted]
- masklinn 12y ago> This does get a little uglier if the intent is that you have multiple '.something' elements, though. That part of the DOM API will always be pretty gross compared to jQuery, I think. Well yeah hence my remark. Working on a single node in a single modern browser, the snippet will turn into var node = document.querySelector('.something'); node.classList.toggle('collapsed') node.addEventListener('click', somethingClicked); but if you need multiple nodes, now you have to manually loop and beware your closures. And that's a best-case scenario, then you start traversing the DOM tree, adding elements, using events delegation and changing attributes and things go south fast.
- BinaryIdiot 12y ago> And that's a best-case scenario, then you start traversing the DOM tree, adding elements, using events delegation and changing attributes and things go south fast. Why would things so south? The DOM API, while not very elegant, is very simple and straight forward. So you need some loops because jQuery hides that for you, so what? Why does using the DOM API make you think things go south fast?
- masklinn 12y ago> Why would things so south? The DOM API, while not very elegant It's not just "not very elegant", it's highly inconvenient and horrendously verbose, and generally fails to take advantage of or correctly integrate with the host language as it's originally expressed in a restrictive and alien IDL. > is very simple and straight forward I completely disagree on both count, the API is neither simple nor straightforward, many tasks remain convoluted even with HTML5's additions of a number of long-awaited APIs (e.g.: repeatedly adding children at the start of a possibly child-less element) and the userland code is made unnecessarily complex not by a simple API but by an insufficient one (e.g. enjoy handrolling element-relative events delegation Element#matches is a pointless waste of API space). And then there's the truly idiotic stuff added on top, such as the live nodelists which serve no other purpose than making implementations slower, more complex and event less integrable with the host language. Thank god the WhatWG managed to keep that garbage out of the new APIs they introduced. > So you need some loops because jQuery hides that for you, so what? So that means overly complex user code for no good reason but the base API being garbage. And jQuery does not "hide loops", that's just a side-effect of jQuery working at a higher conceptual level of manipulating nodesets.
- deleted 12y ago[deleted]