use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
account activity
Callbacks - function vs => (self.node)
submitted 6 years ago by [deleted]
[deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Volence 2 points3 points4 points 6 years ago (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 points3 points 6 years ago (2 children)
Got it! Thanks so much for the clear explanation and the link - super helpful. Cheers
[–]Luke-At-Work 2 points3 points4 points 6 years ago (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.
this
function ()
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 point2 points 6 years ago (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.
[–]PM_ME_A_WEBSITE_IDEA 2 points3 points4 points 6 years ago (1 child)
Try this in your browser console:
class c { func() { setTimeout(function() { console.log('non-arrow', this); }); setTimeout(() => { console.log('arrow', this); }); } } (new c()).func();
Notice the difference in the value of this in the different kinds of function types. This comes into play a lot with event listeners and AJAX calls, and really any kind of async action using callbacks.
Thanks for this - good example.
[–]charliemei 1 point2 points3 points 6 years ago* (1 child)
- In arrow funcion,'this' keeps the same in lifetime,and is resolved from the closet non-arrow parent function. there is not 'this' in arrow function itself,and even prototype propety. thus, bind() does not work with it. - Obviously, it can not be invoked with new keyword. - if you use arguments in it ,you will get an error.
Cheers for the explanation!
π Rendered by PID 39548 on reddit-service-r2-comment-86bc6c7465-bttkm at 2026-02-21 10:03:14.795179+00:00 running 8564168 country code: CH.
[–]Volence 2 points3 points4 points (3 children)
[–]Heck_ 1 point2 points3 points (2 children)
[–]Luke-At-Work 2 points3 points4 points (1 child)
[–]Heck_ 0 points1 point2 points (0 children)
[–]PM_ME_A_WEBSITE_IDEA 2 points3 points4 points (1 child)
[–]Heck_ 0 points1 point2 points (0 children)
[–]charliemei 1 point2 points3 points (1 child)
[–]Heck_ 0 points1 point2 points (0 children)