5 ms·
Yes but that’s an anti pattern. Arrow functions aren’t there to look cool, they’re how you define lambdas / anonymous functions. Other than that, functions sho
by pjerem 2y ago
Yes but that’s an anti pattern. Arrow functions aren’t there to look cool, they’re how you define lambdas / anonymous functions.
Other than that, functions should be defined by the keyword.
- bobbylarrybobby 2y agoFunctions and arrow functions have an important difference: arrow functions do not create their own `this`. If you're in a context where a nested function needs to maintain access to the outer function’s `this`, and you don't want to muck with `bind` or `call`, then you need an arrow function.
- wiseowise 2y agoHow is that an anti-pattern? > Other than that, functions should be defined by the keyword. Says who?
- lukan 2y agoAll the wise ones. Well, except for you maybe. Serious arguments would be: - readability - greppability
- lukan 2y ago(It wasn't an insult, but a joke on the username)
- tylerhou 2y agoAs of a few years ago (not sure about now) the backtrace frame info for anonymous functions were far worse than ones defined via the function keyword with a name.
- hansworst 2y agoAnonymous functions don't have names. This makes it much harder to do things like profiling (just try to find that one specific arrow function in your performance profile flame graph) and tracing. Tools like Sentry that automatically log stack traces when errors occur become much less useful if every function is anonymous.
- medstrom 2y agoconst foo = () => {} This function is not anonymous, it's called foo.
- BlarfMcFlarf 2y agoDoes the function know it’s called foo for tracing/error logging/etc?
- croes 2y agoBut to call foo in bar you must define foo before bar. function foo(){} is also callable if bar is defined before foo.
- sestep 2y agoNot true at the top-level.
- wruza 2y agoNot sure what you find not true about it. All named “function”s get hoisted just like “var”s, I use post-definitions of utility functions all the time in file scopes, function scopes, after return statements, everywhere. You’re probably thinking about const foo = function (){} without its own name before (). These behave like expressions and cannot be hoisted.
- MetaWhirledPeas 2y ago> I use post-definitions of utility functions all the time in file scopes, function scopes, after return statements, everywhere I haven't figured out if people consider this a best practice, but I love doing it. To me the list of called functions is a high-level explanation of the code, and listing all the definitions first just buries the high-level logic "below the fold". Immediately diving into function contents outside of their broader context is confusing to me.
- deleted 2y ago[deleted]