JSLint Beta error

For the following JS function:

function cL() {

  'use strict';

  return console.log.apply(console, arguments);

}

      

I am getting the following JSLint Beta error:

Unexpected 'arguments'.
  return console.log.apply(console, arguments);

      

In the old version of JSLint, I never got this error.
Why doesn't the new JSLint Beta like this, and what can I do to get rid of this error? /

Thank.

+3


source to share


1 answer


It will take me a little while to dig up the changes that catch this now in the new JSLint beta. After saving things, it's the same since last July. Crockford just changed jslint.js like crazy about a week ago when the beta crashed . I think we know why he was silent for so long.

But here's a quick version while I'm digging the exact spot in the code: JSLint doesn't want you depending on arguments

at all. This makes the code (according to JSLint) more complex.

Let's say we changed your code to this:

/*jslint white:true, devel:true */
function cL() {
  'use strict';
  console.log(arguments[0]);
  return console.log.apply(console, arguments);
}

      

Even with old JSLint you get:

Use a named parameter.
  console.log(arguments[0]);

      

JSLint wants the code to be changed to useable named parameters, at least ...

/*jslint white:true, devel:true */
function cL(p) {
  'use strict';
  console.log(p[0]);
  return console.log.apply(console, p);
}

      

This also works in beta. Note that I also changed (natch) arguments

in your original call to p

. * See what that means in the note below JSLint doesn't skip things, and Crockford rolls those misses as they are brought to his attention.

I don't believe there is a directive to ignore this issue, but the fix is ​​pretty painless.

Just fwiw since it makes it easier for me to understand the JSLint mentality, anyway remember that the main motivation behind JSLint is ......



When you deliberately write things that look like mistakes, it makes it much harder to find real mistakes.

To find a needle, make your program less like a haystack.

Having no arguments declared and then referencing them through arguments

is a bit haystack-y, at least from a JSLint perspective. And that makes sense. function fn()

at least implies that the function is not using an important parameter; you only need to deal with the closing context. function fn(p)

although the parameter name stinks is more useful. I know that something is going through and will follow the needle. (Insert Arya Stark link).


Note: See the comments below, which I didn't explain the first time around. arguments

is the entire array of arguments passed to the function, which means replacing arguments

with p

in my original function overwrite anything after the first argument.

I assumed the OP could package everything into an array instead of sending it as separate arguments, but that was not an obvious assumption, and I did not make my assumption that this was the correct route.

This is what I mean:

  • Use cL(arg1, arg2, arg3);

    with my version cL(p)

    will only have arg1

    in p

    .
  • var a = [arg1, arg2, arg3];cL(a);

    will give what the OP wants, but takes an array definition first.
  • cL([arg1, arg2, arg3]);

    - this is the same as the previous version, but shorter.

Here's a demo:

/*jslint white:true, devel:true */
function cL(p) {
  'use strict';
  if ("string" === typeof p)    {
    p = [p];
  }
  console.log("first arg: " + p[0]);
  return console.log.apply(console, p);
}

var arg1 = "a",
    arg2 = "b",
    arg3 = "c";

cL(arg1, arg2, arg3);   // only arg1 is passed. 'a' is all that output

var a = [arg1, arg2, arg3];
cL(a);                  // all three argX vars are passed in an array. 'abc'

cL([arg1, arg2, arg3]); // ditto. 'abc'

      

And here's the output (if you're not on IE9 - without opening dev tools):

first arg: a
a
first arg: a
a b c
first arg: a
a b c

      

+2


source







All Articles