Skip to content

Commit f06e0b5

Browse files
camsteffennzakasmdjermanovic
authored
docs: clarify func-style (#18477)
* docs: clarify func-style Fixes #18474 * Update docs/src/rules/func-style.md Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com> --------- Co-authored-by: Nicholas C. Zakas <nicholas@humanwhocodes.com> Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
1 parent 389744b commit f06e0b5

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

docs/src/rules/func-style.md

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,36 @@ further_reading:
55
- https://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html
66
---
77

8-
9-
There are two ways of defining functions in JavaScript: `function` declarations and `function` expressions. Declarations contain the `function` keyword first, followed by a name and then its arguments and the function body, for example:
8+
There are two ways of defining functions in JavaScript: `function` declarations and function expressions assigned to variables. Function declarations are statements that begin with the `function` keyword. Function expressions can either be arrow functions or use the `function` keyword with an optional name. Here are some examples:
109

1110
```js
11+
// function declaration
1212
function doSomething() {
1313
// ...
1414
}
15-
```
1615

17-
Equivalent function expressions begin with the `var` keyword, followed by a name and then the function itself, such as:
16+
// arrow function expression assigned to a variable
17+
const doSomethingElse = () => {
18+
// ...
19+
};
1820

19-
```js
20-
var doSomething = function() {
21+
// function expression assigned to a variable
22+
const doSomethingAgain = function() {
2123
// ...
2224
};
2325
```
2426

25-
The primary difference between `function` declarations and `function expressions` is that declarations are *hoisted* to the top of the scope in which they are defined, which allows you to write code that uses the function before its declaration. For example:
27+
The primary difference between `function` declarations and function expressions is that declarations are *hoisted* to the top of the scope in which they are defined, which allows you to write code that uses the function before its declaration. For example:
2628

2729
```js
28-
doSomething();
30+
doSomething(); // ok
2931

3032
function doSomething() {
3133
// ...
3234
}
3335
```
3436

35-
Although this code might seem like an error, it actually works fine because JavaScript engines hoist the `function` declarations to the top of the scope. That means this code is treated as if the declaration came before the invocation.
36-
37-
For `function` expressions, you must define the function before it is used, otherwise it causes an error. Example:
37+
For function expressions, you must define the function before it is used, otherwise it causes an error. Example:
3838

3939
```js
4040
doSomething(); // error!
@@ -50,7 +50,9 @@ Due to these different behaviors, it is common to have guidelines as to which st
5050

5151
## Rule Details
5252

53-
This rule enforces a particular type of `function` style throughout a JavaScript file, either declarations or expressions. You can specify which you prefer in the configuration.
53+
This rule enforces a particular type of function style, either `function` declarations or expressions assigned to variables. You can specify which you prefer in the configuration.
54+
55+
Note: This rule does not apply to *all* functions. For example, a callback function passed as an argument to another function is not considered by this rule.
5456

5557
## Options
5658

lib/rules/func-style.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = {
1414
type: "suggestion",
1515

1616
docs: {
17-
description: "Enforce the consistent use of either `function` declarations or expressions",
17+
description: "Enforce the consistent use of either `function` declarations or expressions assigned to variables",
1818
recommended: false,
1919
url: "https://eslint.org/docs/latest/rules/func-style"
2020
},

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy