[guide] Update declarations section to prefer expressions.

Fixes #1008.
This commit is contained in:
Jordan Harband
2016-09-06 23:08:36 -07:00
parent 7f12b9f043
commit ccd18f8b40
2 changed files with 10 additions and 4 deletions

View File

@@ -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 its easy - too easy - to reference the function before it is defined in the file. This harms readability and maintainability. If you find that a functions definition is large or complex enough that it is interfering with understanding the rest of the file, then perhaps its time to extract it to its own module! Dont 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>

View File

@@ -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