7 ms·
This only supports IE10+. Why shouldn't I just use Javascript directly?
by hackerboos 12y ago
This only supports IE10+. Why shouldn't I just use Javascript directly?
- Jgrubb 12y agoNot the author, but the API still sucks to write using vanilla JS. There are also still plenty of inconsistencies in modern browsers (per an article floating around here in the last day or two).
- smt88 12y ago> the API still sucks If you want syntactic sugar, a better alternative to a client-side library (which has overhead) is to use something like TypeScript (or one of the other compile-to-JavaScript options).
- anentropic 12y agothey solve different problems though... syntactic sugar from TypeScript et al solve problems with Javascript the language jQuery solve problems with the various builtin APIs like DOM etc that are part of either Javascript or the web browser host
- smt88 12y agoI just assumed the person I was replying to was complaining about document.getElementById('whatever') (etc.) vs $('#whatever'). In response to your comment, polyfills are more future-proof solutions to browser compatibility, which is the only remaining problem that I see jQuery as solving well.
- Jgrubb 12y ago> I just assumed the person I was replying to was complaining about document.getElementById('whatever') (etc.) vs $('#whatever') Actually that is what I was referring to. I must not have given TypeScript enough of a shake. I couldn't find the section in the early tutorial that showed me that sugar, and I also couldn't figure out how the typing stuff would help me working with a bunch of network-y type apps. Obviously this is my own failing. Inspired now to give it another shot, so thanks.
- smt88 12y agoIt's entirely my own fault, but this has been a really muddled conversation. When I mentioned TypeScript, I only meant that it (and other languages that compile to JavaScript, like Go) is a better syntax overall. I didn't actually mean that it helps with DOM selection/manipulation. So if you want short DOM syntax, I'd suggest using vanilla ES6[1] with polyfills, and then figure out what's really wasting your time. Then, you can write your own sugar for it. In my opinion, document.getElementById is something that you end up writing a lot and is really long, but with autocomplete in a good IDE, it's not a big deal. Plus, any JS developer who sees it will know exactly what it does and what it returns. 1. https://babeljs.io https://babeljs.io
- masklinn 12y agoThe 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.