admin管理员组

文章数量:1415673

As of about 14 January 2016, JSLint has started plaining about var or let declarations that have more than one variable per declaration, and has created a new directive, multivar that ignores this new "problem".

This is a pretty significant change, as earlier versions would plain if you did have two vars in the same code block.

That is, as of today (18 Jan 2016), this code now breaks in JSLint:

/*jslint white:true, browser:true, devel:true */
function a(b) {
    "use strict";
    var c, d; // <<< Now bad!!
    d = "test";
    c = d + b;
    console.log(c);
}

The error reported is, Expected ';' and instead saw ','. for the line var c,d;

The "correct" fix is apparently this:

/*jslint white:true, browser:true, devel:true */
function a(b) {
    "use strict";
    var c;
    var d;  // <<< this *used* to be forbidden.
    d = "test";
    c = d + b;
    console.log(c);
}

Note that this newly correct code would have produced the error, Error: bine_var, in earlier versions of JSLint.

The only descriptions for the change from Crockford that I can find seem to be this post on Google Plus:

JSLint now has a multivar option that tolerates multiple names being declared in a single var, let, or const statement.

... and a quick mention on the website instructions...

Tolerate multiple variables declarations per statement
multivar
true if a var, let, or const statement can declare two or more variables in a single statement.

The multivar change on JSLint doesn't seem to be in the GitHub repo yet. See the two checkins (1, 2) entitled "var" for 14 Jan. Those two make the code in JSLint follow the new requirement, but don't (afaict) add the multivar directive described and in use on JSLint.

Can anybody tell me why multiple var lines are encouraged/required now, other than the usual, "You should ignore JSLint," answers? That is, why was a single var required before (I'd guess to encourage an understanding of hoisting), and why is that reasoning suddenly moot?

As of about 14 January 2016, JSLint has started plaining about var or let declarations that have more than one variable per declaration, and has created a new directive, multivar that ignores this new "problem".

This is a pretty significant change, as earlier versions would plain if you did have two vars in the same code block.

That is, as of today (18 Jan 2016), this code now breaks in JSLint:

/*jslint white:true, browser:true, devel:true */
function a(b) {
    "use strict";
    var c, d; // <<< Now bad!!
    d = "test";
    c = d + b;
    console.log(c);
}

The error reported is, Expected ';' and instead saw ','. for the line var c,d;

The "correct" fix is apparently this:

/*jslint white:true, browser:true, devel:true */
function a(b) {
    "use strict";
    var c;
    var d;  // <<< this *used* to be forbidden.
    d = "test";
    c = d + b;
    console.log(c);
}

Note that this newly correct code would have produced the error, Error: bine_var, in earlier versions of JSLint.

The only descriptions for the change from Crockford that I can find seem to be this post on Google Plus:

JSLint now has a multivar option that tolerates multiple names being declared in a single var, let, or const statement.

... and a quick mention on the website instructions...

Tolerate multiple variables declarations per statement
multivar
true if a var, let, or const statement can declare two or more variables in a single statement.

The multivar change on JSLint. doesn't seem to be in the GitHub repo yet. See the two checkins (1, 2) entitled "var" for 14 Jan. Those two make the code in JSLint follow the new requirement, but don't (afaict) add the multivar directive described and in use on JSLint..

Can anybody tell me why multiple var lines are encouraged/required now, other than the usual, "You should ignore JSLint," answers? That is, why was a single var required before (I'd guess to encourage an understanding of hoisting), and why is that reasoning suddenly moot?

Share Improve this question edited Mar 15, 2016 at 12:40 ruffin asked Jan 18, 2016 at 19:28 ruffinruffin 17.5k11 gold badges96 silver badges149 bronze badges 5
  • If you're going to vote down and/or vote to close, a ment why would be awfully helpful. Thanks! – ruffin Commented Jan 18, 2016 at 19:42
  • 1 Not sure about the downvote, but that's a very radical change, I definitely didn't expect that (especially since it's disabled by default). – Josh Crozier Commented Jan 18, 2016 at 19:45
  • 3 Hey @JoshCrozier! Yep, surprised the heck out of me too. It's not just a change, like when JSLint remended we stop using for loops recently, it's a flat 180 from what was required previously. – ruffin Commented Jan 18, 2016 at 19:46
  • 1 Looks like mr. Crockford has rewritten his Code conventions page too. – Teemu Commented Jan 18, 2016 at 19:47
  • 1 @Teemu Great catch. He does say there, "It is preferred that each variable declarative statement [be on its own line?] and [include a?] ment". Unf, even that page doesn't follow this new rule throughout... cf. Function Declarations -- there's no "use strict"' and there's a var array, ncn = node.className;! I get a Expected ';' and instead saw ','. when I lint it. At least that shows this is pretty new, I guess. – ruffin Commented Jan 18, 2016 at 19:52
Add a ment  | 

1 Answer 1

Reset to default 3

From the AirBNB style guide.

It's easier to add new variable declarations this way, and you never have to worry about swapping out a ; for a , or introducing punctuation-only diffs. You can also step through each declaration with the debugger, instead of jumping through all of them at once.

More on this at: https://github./airbnb/javascript#variables

本文标签: javascriptExpected 3939 and instead saw 3939JSLint multivar settingStack Overflow