Search code examples
javascriptnode.jsargumentsjslint

JSLint Unexpected 'arguments'


Is there a JSLint comment that can be declared to ignore the warning? Also, why does it warn about the error? I recall reading somewhere they are not true arrays, so coders had to get the arguments length and pass the values by a for loop into an array for some reason.

This is one way I use arguments posted here. I also use it for console.log for perimeter passing tests.


Solution

  • I don't know whether or not the warning can be ignored or exactly why Crockford doesn't like the arguments object, so this won't be a great answer.

    Here's what I do know: ES6 provides the rest parameter, which gives us a much cleaner way to get nearly the same functionality as with the arguments object (there are a few differences). Also, JSLint won't give you any warnings for it (unless you aren't assuming ES6).

    For example, if you wanted to write a sum function, with the arguments object your code might look like this:

    function sum() {
        const args = Array.prototype.slice.call(arguments, 0);
        return args.reduce(function (a, b) {
            return a + b;
        });
    }
    

    With the rest parameter, you could write the sum function like this:

    function sum(...args) {
        return args.reduce(function (a, b) {
            return a + b;
        });
    }