mirror of
https://github.com/airbnb/javascript.git
synced 2026-01-14 06:18:00 -05:00
[guide] Update declarations section to prefer expressions.
Fixes #1008.
This commit is contained in:
10
README.md
10
README.md
@@ -554,18 +554,22 @@ Other Style Guides
|
||||
## Functions
|
||||
|
||||
<a name="functions--declarations"></a><a name="7.1"></a>
|
||||
- [7.1](#functions--declarations) Use function declarations instead of function expressions. eslint: [`func-style`](http://eslint.org/docs/rules/func-style) jscs: [`requireFunctionDeclarations`](http://jscs.info/rule/requireFunctionDeclarations)
|
||||
- [7.1](#functions--declarations) Use named function expressions instead of function declarations. eslint: [`func-style`](http://eslint.org/docs/rules/func-style) jscs: [`requireFunctionDeclarations`](http://jscs.info/rule/requireFunctionDeclarations)
|
||||
|
||||
> Why? Function declarations are named, so they're easier to identify in call stacks. Also, the whole body of a function declaration is hoisted, whereas only the reference of a function expression is hoisted. This rule makes it possible to always use [Arrow Functions](#arrow-functions) in place of function expressions. ([discussion](https://github.com/airbnb/javascript/issues/794))
|
||||
> Why? Function declarations are hoisted, which means that it’s easy - too easy - to reference the function before it is defined in the file. This harms readability and maintainability. If you find that a function’s definition is large or complex enough that it is interfering with understanding the rest of the file, then perhaps it’s time to extract it to its own module! Don’t forget to name the expression - anonymous functions can make it harder to locate the problem in an Error's call stack.
|
||||
|
||||
```javascript
|
||||
// bad
|
||||
const foo = function () {
|
||||
};
|
||||
|
||||
// good
|
||||
// bad
|
||||
function foo() {
|
||||
}
|
||||
|
||||
// good
|
||||
const foo = function bar() {
|
||||
};
|
||||
```
|
||||
|
||||
<a name="functions--iife"></a><a name="7.2"></a>
|
||||
|
||||
@@ -36,7 +36,9 @@ module.exports = {
|
||||
'func-names': 'warn',
|
||||
|
||||
// enforces use of function declarations or expressions
|
||||
'func-style': 'off',
|
||||
// http://eslint.org/docs/rules/func-style
|
||||
// TODO: enable
|
||||
'func-style': ['off', 'expression'],
|
||||
|
||||
// Blacklist certain identifiers to prevent them being used
|
||||
// http://eslint.org/docs/rules/id-blacklist
|
||||
|
||||
Reference in New Issue
Block a user