]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/prefer-rest-params.md
first commit
[pve-eslint.git] / eslint / docs / rules / prefer-rest-params.md
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
17 function foo() {
18 console.log(arguments);
19 }
20
21 function foo(action) {
22 var args = Array.prototype.slice.call(arguments, 1);
23 action.apply(null, args);
24 }
25
26 function foo(action) {
27 var args = [].slice.call(arguments, 1);
28 action.apply(null, args);
29 }
30 ```
31
32 Examples of **correct** code for this rule:
33
34 ```js
35 function foo(...args) {
36 console.log(args);
37 }
38
39 function foo(action, ...args) {
40 action.apply(null, args); // or `action(...args)`, related to the `prefer-spread` rule.
41 }
42
43 // Note: the implicit arguments can be overwritten.
44 function foo(arguments) {
45 console.log(arguments); // This is the first argument.
46 }
47 function foo() {
48 var arguments = 0;
49 console.log(arguments); // This is a local variable.
50 }
51 ```
52
53 ## When Not To Use It
54
55 This rule should not be used in ES3/5 environments.
56
57 In ES2015 (ES6) or later, if you don't want to be notified about `arguments` variables, then it's safe to disable this rule.
58
59 ## Related Rules
60
61 * [prefer-spread](prefer-spread.md)