Skip to content
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,45 @@ Other Style Guides

> Why not? If you have a fairly complicated function, you might move that logic out into its own named function expression.

> **Note:** While arrow functions are recommended for anonymous callbacks, traditional
> function declarations or expressions are still preferred in certain scenarios where
> their behavior is more appropriate.
>
> **Prefer traditional functions when:**
>
> - You rely on **function hoisting**, such as calling a function before its definition.
Comment thread
Manvendra0023 marked this conversation as resolved.
Outdated
> - You need a **dynamic `this` binding**, for example in object methods or DOM event handlers.
> - You require access to the **`arguments` object**, which is not available in arrow functions.
>
> **Examples:**
>
> ```js
> // Function hoisting
> initialize();
>
> function initialize() {
> // setup logic
> }
> ```
>
> ```js
> // Dynamic `this` binding (e.g. event handlers)
> const button = document.querySelector('button');
>
> button.addEventListener('click', function () {
> this.classList.add('active');
Comment on lines +975 to +979
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is actually not a good idea; in event handlers, you should use the event object argument's target or currentTarget instead of this.

> });
> ```
>
> ```js
> // Using the `arguments` object
> function sum() {
> return Array.from(arguments).reduce((total, value) => total + value, 0);
> }
> ```



```javascript
// bad
[1, 2, 3].map(function (x) {
Expand Down