Array methods should not be called on the arguments
object itself
- BAD_ARRAY_METHOD_ON_ARGUMENTS
- Error
- High
- No tags
This rule applies when an array method is called on the arguments
object itself.
The arguments
object is not an array, but an array-like object. It should be converted to a real array before calling an array method. Otherwise, a TypeError
exception will be thrown because of the non-existent method.
In general, instead of using arguments
, it is recommended to use rest parameters which are real Array
objects.
Noncompliant Code Example
View with compliant examples side by sidefunction add(x, y) {
return x + y;
}
function sum() {
return arguments.reduce(add, 0); // BAD_ARRAY_METHOD_ON_ARGUMENTS alarm because 'reduce()' is an array method.
}
Compliant Code Example
View with noncompliant examples side by sidefunction add(x, y) {
return x + y;
}
function sum(...args) { // Use rest parameter instead of 'arguments'
return args.reduce(add, 0);
}
Version
This rule was introduced in DeepScan 1.32.0.