-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Lint warning about `arguments`
arguments
is a variable that is in scope in every function, which
more or less acts like an array, but unfortunately is not an array.
There is a lint check that warns about many usages of the arguments
variable.
If you're iterating over it in a for loop, or accessing specific elements of it,
there is no problem. But if you're using Array.prototype.slice
to convert it
to an Array, or passing it to another function, the linter will give you a
warning that looks like:
WARNING - This use of the arguments object is discouraged.
var args = Array.prototype.slice.call(arguments);
^
These usages are discouraged for the following reasons:
-
If you pass the Arguments object to another function, it can result in a significant slowdown. See discussion on this bug for more details.
-
If you pass the Arguments object to another function and then use ES6 destructuring, or use the object in a for/of loop, the compiler's ES6-to-ES3 transpilation will not handle it correctly. For example,
function f(x) { return [...x]; } function g() { f(arguments); }
will transpile to
function f(x) { return [].concat($jscomp.arrayFromIterable(x)); } function g() { f(arguments); }
and
$jscomp.arrayFromIterable
will not successfully convert the Arguments object into an Iterable in pre-ES6 browsers.
We make an exception for Function.prototype.apply
so there is no warning for
f.apply(obj, arguments);
.