]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | # disallow reassigning `function` declarations (no-func-assign) |
2 | ||
3 | JavaScript functions can be written as a FunctionDeclaration `function foo() { ... }` or as a FunctionExpression `var foo = function() { ... };`. While a JavaScript interpreter might tolerate it, overwriting/reassigning a function written as a FunctionDeclaration is often indicative of a mistake or issue. | |
4 | ||
5 | ```js | |
6 | function foo() {} | |
7 | foo = bar; | |
8 | ``` | |
9 | ||
10 | ## Rule Details | |
11 | ||
12 | This rule disallows reassigning `function` declarations. | |
13 | ||
14 | Examples of **incorrect** code for this rule: | |
15 | ||
16 | ```js | |
17 | /*eslint no-func-assign: "error"*/ | |
18 | ||
19 | function foo() {} | |
20 | foo = bar; | |
21 | ||
22 | function foo() { | |
23 | foo = bar; | |
24 | } | |
6f036462 TL |
25 | |
26 | var a = function hello() { | |
27 | hello = 123; | |
28 | }; | |
eb39fafa DC |
29 | ``` |
30 | ||
31 | Examples of **incorrect** code for this rule, unlike the corresponding rule in JSHint: | |
32 | ||
33 | ```js | |
34 | /*eslint no-func-assign: "error"*/ | |
35 | ||
36 | foo = bar; | |
37 | function foo() {} | |
38 | ``` | |
39 | ||
40 | Examples of **correct** code for this rule: | |
41 | ||
42 | ```js | |
43 | /*eslint no-func-assign: "error"*/ | |
44 | ||
45 | var foo = function () {} | |
46 | foo = bar; | |
47 | ||
48 | function foo(foo) { // `foo` is shadowed. | |
49 | foo = bar; | |
50 | } | |
51 | ||
52 | function foo() { | |
53 | var foo = bar; // `foo` is shadowed. | |
54 | } | |
55 | ``` |