]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/func-style.md
c197403d9896ad1407a0641d04e316cddb7644c3
[pve-eslint.git] / eslint / docs / rules / func-style.md
1 # enforce the consistent use of either `function` declarations or expressions (func-style)
2
3 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:
4
5 ```js
6 function doSomething() {
7 // ...
8 }
9 ```
10
11 Equivalent function expressions begin with the `var` keyword, followed by a name and then the function itself, such as:
12
13 ```js
14 var doSomething = function() {
15 // ...
16 };
17 ```
18
19 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:
20
21 ```js
22 doSomething();
23
24 function doSomething() {
25 // ...
26 }
27 ```
28
29 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.
30
31 For `function` expressions, you must define the function before it is used, otherwise it causes an error. Example:
32
33 ```js
34 doSomething(); // error!
35
36 var doSomething = function() {
37 // ...
38 };
39 ```
40
41 In this case, `doSomething()` is undefined at the time of invocation and so causes a runtime error.
42
43 Due to these different behaviors, it is common to have guidelines as to which style of function should be used. There is really no correct or incorrect choice here, it is just a preference.
44
45 ## Rule Details
46
47 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.
48
49 ## Options
50
51 This rule has a string option:
52
53 * `"expression"` (default) requires the use of function expressions instead of function declarations
54 * `"declaration"` requires the use of function declarations instead of function expressions
55
56 This rule has an object option for an exception:
57
58 * `"allowArrowFunctions"`: `true` (default `false`) allows the use of arrow functions. This option applies only when the string option is set to `"declaration"` (arrow functions are always allowed when the string option is set to `"expression"`, regardless of this option)
59
60 ### expression
61
62 Examples of **incorrect** code for this rule with the default `"expression"` option:
63
64 ```js
65 /*eslint func-style: ["error", "expression"]*/
66
67 function foo() {
68 // ...
69 }
70 ```
71
72 Examples of **correct** code for this rule with the default `"expression"` option:
73
74 ```js
75 /*eslint func-style: ["error", "expression"]*/
76
77 var foo = function() {
78 // ...
79 };
80
81 var foo = () => {};
82
83 // allowed as allowArrowFunctions : false is applied only for declaration
84 ```
85
86 ### declaration
87
88 Examples of **incorrect** code for this rule with the `"declaration"` option:
89
90 ```js
91 /*eslint func-style: ["error", "declaration"]*/
92
93 var foo = function() {
94 // ...
95 };
96
97 var foo = () => {};
98 ```
99
100 Examples of **correct** code for this rule with the `"declaration"` option:
101
102 ```js
103 /*eslint func-style: ["error", "declaration"]*/
104
105 function foo() {
106 // ...
107 }
108
109 // Methods (functions assigned to objects) are not checked by this rule
110 SomeObject.foo = function() {
111 // ...
112 };
113 ```
114
115 ### allowArrowFunctions
116
117 Examples of additional **correct** code for this rule with the `"declaration", { "allowArrowFunctions": true }` options:
118
119 ```js
120 /*eslint func-style: ["error", "declaration", { "allowArrowFunctions": true }]*/
121
122 var foo = () => {};
123 ```
124
125 ## When Not To Use It
126
127 If you want to allow developers to each decide how they want to write functions on their own, then you can disable this rule.
128
129 ## Further Reading
130
131 * [JavaScript Scoping and Hoisting](http://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html)