Docs: clarify cases where traditional functions are preferred - #3221
Docs: clarify cases where traditional functions are preferred#3221Manvendra0023 wants to merge 4 commits into
Conversation
ljharb
left a comment
There was a problem hiding this comment.
Arrow functions should only be used for inline callbacks. Everything else should use normal functions.
|
Thanks for the clarification β that makes sense. I agree that this guide should not encourage relying on hoisting, and that arrow functions should generally be preferred except for inline callbacks. My intent here wasnβt to recommend hoisting as a practice, but to document why traditional functions still exist and what technical differences remain, for readers who may encounter them in real codebases. Iβm happy to remove or reword the hoisting mention to avoid implying itβs an acceptable pattern under this guide. Please let me know if youβd prefer the section to focus only on cases like named functions for debugging / stack traces, or if youβd rather drop the section entirely. |
|
I think that instead of "here's when you shouldn't use arrow functions", i think more useful guidance is "here are the only places you should use arrow functions". |
|
That makes sense β I agree that framing this in terms of βwhere arrow functions should be usedβ is clearer and more in line with the guideβs intent. Iβll update the section to focus on a short, explicit list of recommended arrow function use cases (e.g. inline callbacks), and remove or de-emphasise the βwhen not to useβ framing. Iβll push an update shortly β thanks for the guidance. |
Co-authored-by: Jordan Harband <ljharb@gmail.com>
|
Thanks for the suggestion! Applied π |
| > // Dynamic `this` binding (e.g. event handlers) | ||
| > const button = document.querySelector('button'); | ||
| > | ||
| > button.addEventListener('click', function () { | ||
| > this.classList.add('active'); |
There was a problem hiding this comment.
this is actually not a good idea; in event handlers, you should use the event object argument's target or currentTarget instead of this.
This PR adds a small clarification to the Arrow Functions section describing cases where traditional functions are still preferred, such as function hoisting, dynamic
thisbinding, and use of theargumentsobject.The goal is to improve clarity without changing the existing recommendation.