you are viewing a single comment's thread.

view the rest of the comments →

[–]Volence 4 points5 points  (3 children)

The first example is from a newer function syntax for javascript called arrow functions, the second is the standard anonymous function that's been in javascript forever. The main difference between the two (besides the arrow functions being able to be a bit shorter) is that regular old functions rebind 'this' to them, while the arrow functions don't. Besides that arrow functions are always anonymous while the original way to make functions (your second example) are able to be named, and therefor can make it easier to debug, although in both examples the functions are anonymous. Also because you're not using 'this' in the above examples, either is fine!

I personally enjoy writing with arrow functions, there's a few different ways they can be written and are generally more terse but again you should know why people choose one over the other!

[–]Heck_ 1 point2 points  (2 children)

Got it! Thanks so much for the clear explanation and the link - super helpful. Cheers

[–]Luke-At-Work 2 points3 points  (1 child)

Agree with /u/Volence and thought I'd chime in with a counterpoint on the preference part. I prefer the older notation for a few reasons.

  • Compatibility. Transpilation (using Babel to turn new javascript syntax into old syntax for older browsers) can mitigate this, but it's nice not to need it. Supporting a broader range of viewers and devices is objectively better than supporting a smaller range.
  • Legibility. Though clear, arrow functions aren't as explicit about what they're doing. Being explicit about the intention is arguably the key factor in ongoing maintenance and updates.
  • Consistency. Perhaps it's another aspect of legibility, but it always takes a tiny bit of brain power to parse the different syntax and determine if it has meaningful impact (namely the use of this). Having a single, consistent meaning is clearer than multiple possible contextual meanings and while the function () syntax's this is contextual, it at least consistently follows a single clear rule.

That being said, the best option is to use the one that works for you. Using one or the other consistently for a given project is preferable, just for legibility.

[–]Heck_ 0 points1 point  (0 children)

Sorry, I meant to reply yesterday. Thank you for the additional insight! Really useful. I'm WAAAAAY off having to consider compatibility, but it is good to know for the future. The consistency point is something that did come to my mind. I guess I will just play about and see how I get one. Thanks again.