]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | # Suggest using the rest parameters instead of `arguments` (prefer-rest-params) |
2 | ||
3 | There are rest parameters in ES2015. | |
4 | We can use that feature for variadic functions instead of the `arguments` variable. | |
5 | ||
6 | `arguments` does not have methods of `Array.prototype`, so it's a bit of an inconvenience. | |
7 | ||
8 | ## Rule Details | |
9 | ||
10 | This rule is aimed to flag usage of `arguments` variables. | |
11 | ||
12 | ## Examples | |
13 | ||
14 | Examples of **incorrect** code for this rule: | |
15 | ||
16 | ```js | |
56c4a2cb DC |
17 | /*eslint prefer-rest-params: "error"*/ |
18 | ||
eb39fafa DC |
19 | function foo() { |
20 | console.log(arguments); | |
21 | } | |
22 | ||
23 | function foo(action) { | |
24 | var args = Array.prototype.slice.call(arguments, 1); | |
25 | action.apply(null, args); | |
26 | } | |
27 | ||
28 | function foo(action) { | |
29 | var args = [].slice.call(arguments, 1); | |
30 | action.apply(null, args); | |
31 | } | |
32 | ``` | |
33 | ||
34 | Examples of **correct** code for this rule: | |
35 | ||
36 | ```js | |
56c4a2cb DC |
37 | /*eslint prefer-rest-params: "error"*/ |
38 | ||
eb39fafa DC |
39 | function foo(...args) { |
40 | console.log(args); | |
41 | } | |
42 | ||
43 | function foo(action, ...args) { | |
44 | action.apply(null, args); // or `action(...args)`, related to the `prefer-spread` rule. | |
45 | } | |
46 | ||
47 | // Note: the implicit arguments can be overwritten. | |
48 | function foo(arguments) { | |
49 | console.log(arguments); // This is the first argument. | |
50 | } | |
51 | function foo() { | |
52 | var arguments = 0; | |
53 | console.log(arguments); // This is a local variable. | |
54 | } | |
55 | ``` | |
56 | ||
57 | ## When Not To Use It | |
58 | ||
59 | This rule should not be used in ES3/5 environments. | |
60 | ||
61 | In ES2015 (ES6) or later, if you don't want to be notified about `arguments` variables, then it's safe to disable this rule. | |
62 | ||
63 | ## Related Rules | |
64 | ||
65 | * [prefer-spread](prefer-spread.md) |