]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/rules/prefer-rest-params.md
bump version to 8.4.0-3
[pve-eslint.git] / eslint / docs / rules / prefer-rest-params.md
CommitLineData
eb39fafa
DC
1# Suggest using the rest parameters instead of `arguments` (prefer-rest-params)
2
3There are rest parameters in ES2015.
4We 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
10This rule is aimed to flag usage of `arguments` variables.
11
12## Examples
13
14Examples of **incorrect** code for this rule:
15
16```js
56c4a2cb
DC
17/*eslint prefer-rest-params: "error"*/
18
eb39fafa
DC
19function foo() {
20 console.log(arguments);
21}
22
23function foo(action) {
24 var args = Array.prototype.slice.call(arguments, 1);
25 action.apply(null, args);
26}
27
28function foo(action) {
29 var args = [].slice.call(arguments, 1);
30 action.apply(null, args);
31}
32```
33
34Examples of **correct** code for this rule:
35
36```js
56c4a2cb
DC
37/*eslint prefer-rest-params: "error"*/
38
eb39fafa
DC
39function foo(...args) {
40 console.log(args);
41}
42
43function 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.
48function foo(arguments) {
49 console.log(arguments); // This is the first argument.
50}
51function foo() {
52 var arguments = 0;
53 console.log(arguments); // This is a local variable.
54}
55```
56
57## When Not To Use It
58
59This rule should not be used in ES3/5 environments.
60
61In 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)