r/programming • • Jun 09 '16

jQuery 3.0 Released!

https://blog.jquery.com/2016/06/09/jquery-3-0-final-released/
1.1k Upvotes

354 comments sorted by

View all comments

43

u/earthboundkid Jun 09 '16

Most of what I used jQuery for was working around browser incompatibility. As IE11 becomes the trailing edge browser we need to support, the need for jQuery shrinks to zero. Once fetch becomes universal, there's not really anything left.

128

u/EternalNY1 Jun 10 '16 edited Jun 10 '16

As IE11 becomes the trailing edge browser we need to support, the need for jQuery shrinks to zero.

Not true, because they can slim down jQuery (as you see with 3.0) which still allows you to turn what might be 20 lines of ECMA-compliant JavaScript into a one-liner that even assures you much better compatibility.

It's still useful, as long as it's not bloated, and fast.

It makes your own code more readable ... the whole "DRY principle".

This?

var d = document.getElementsByClassName("goodbye");
var i; 
for (i = 0; i < d.length; i++) { 
  d[i].className = d[i].className + " selected";
}

Or?

$(".goodbye").addClass("selected");

12

u/puhnitor Jun 10 '16 edited Jun 10 '16
[...document.querySelectorAll('.goodbye')].forEach(goodbye => goodbye.classList.add('selected'));

80

u/EternalNY1 Jun 10 '16

forEach

Yes if you want all versions of IE dropped as well as Safari.

Or bring in jQuery, small and fast and almost guaranteed to be cached locally anyway, and get it fully compatible in 1 (shorter) line without the lambda "arrow function".

So, in your case, we'd have (works on some browsers):

[...(document.querySelectorAll('.goodbye')].forEach(goodbye => goodbye.classList.add('selected'));

Works on almost all browsers:

$(".goodbye").addClass("selected");

0

u/[deleted] Jun 10 '16

[deleted]

6

u/enkideridu Jun 10 '16

Please don't use maps to perform mutations

1

u/vplatt Jun 10 '16

This is Javascript not Haskell. MUTATE ALL THE THINGS!

(Seriously, I get it. But I strongly suspect it's going to be the "best practice" down the road.)

1

u/enkideridu Jun 11 '16

I don't see how? Could you share your insight on why using map when you should be using forEach might become a best practice?

1

u/vplatt Jun 11 '16

No particular reason other than the fact that I can be a little cynical and part of me believes the community will opt for map simply because it's shorter and "cooler" to use functional programming techniques. The fact that there is nothing in Javascript (that I know of anyway) to enforce referential transparency means that use of the FP techniques will likely be little more than lip service to the concepts.

I'm pretty hard on the Scala and Haskell communities when it comes to being able to make FP practical and accessible for everyday use, but at least they understand how to get the benefits of FP. I currently do not have any such confidence in the Js community.

2

u/enkideridu Jun 11 '16 edited Jun 11 '16

I'm not sure, I might quite possibly have misinterpreted (apologies if that's the case), but just in case, the motivation behind not using map and reduces to perform mutations isn't out of an idealistic "functional programming is good" or "mutations are bad" dogma. It's for the benefit of making your code easier to understand for yourself and others

Specifically, it's an implicit promise that the code inside the function, no matter how complex, can be safely ignored as long as the value it returns is as expected. There is huge value in being able to rely on the (albeit implicit) referential transparency for maps and reduces in the amount of code you can skip when you're combing for bugs or trying to understand a section of a program someone else wrote.

And I can tell you of one way of enforcing referential transparency - code review :)

I've run into a few javascript programmers who've made the same mistake with misusing map (2 juniors and one self-taught rockstar), but after I've explained it the responses were always immediately "that makes sense". That gives me hope that while it may be an easy mistake to make, it won't get touted as a best practice because people will understand why they shouldn't be doing it as soon as you explain it to them, and hopefully pass it forward :)

→ More replies (0)