admin管理员组

文章数量:1399339

Something is wrong with my code or with plovr. I went to JSLint to get some help. However, JSLint seems to consider this as a fatal error and refuses to check more of the code:

for (var i = 0; i < data.length; i += 4) {

Why? I like this way of declaring "I".

JSHint does not seem to be an alternative if you are on Windows. I am trying The Online Lint at the moment. I have a feeling that this bug will be a bit difficult to find so I would be glad for suggestions about good code checkers.

Something is wrong with my code or with plovr. I went to JSLint to get some help. However, JSLint seems to consider this as a fatal error and refuses to check more of the code:

for (var i = 0; i < data.length; i += 4) {

Why? I like this way of declaring "I".

JSHint does not seem to be an alternative if you are on Windows. I am trying The Online Lint at the moment. I have a feeling that this bug will be a bit difficult to find so I would be glad for suggestions about good code checkers.

Share Improve this question edited Jan 2, 2016 at 3:58 user4639281 asked Apr 16, 2014 at 9:16 LeoLeo 4,4387 gold badges54 silver badges84 bronze badges 14
  • 3 My personal opinion is that JSLint is retarded. But opinions may vary on that sensitive topic. – Denys Séguret Commented Apr 16, 2014 at 9:19
  • 2 JSLint is not the gospel regarding how to code JS. Rather, it merely enforces someone's opinion of what constitutes good code. There is nothing wrong with your code. Like the previous menter, I find JSLint far more annoying and anal than it is helpful. – Mitya Commented Apr 16, 2014 at 9:20
  • @Touchpad Please read more about variables in JavaScript. Moving the var declaration to the top of the function doesn't change anything. – Denys Séguret Commented Apr 16, 2014 at 9:20
  • 1 @dystroy JSLint has rules that protect people from shooting themselves in the foot with things that have weird behavior which is not generally very well understood -- If you're one of the guys who perfectly understands that weird behavior perfectly and you work only with people who understand it just as well -- then yeah JSLint is of no use to you, otherwise it may be of use. – Mihai Stancu Commented Apr 16, 2014 at 9:22
  • 1 @dystroy I can relate to the opinion but D. Crockford (of ECMA and JSLint) added it because the behavior of JavaScript is exactly this -- so in a sense by doing what the language already does you're not leaving any room for interpretation. – Mihai Stancu Commented Apr 16, 2014 at 9:27
 |  Show 9 more ments

3 Answers 3

Reset to default 6

I agree with Niet the Dark Absol that the tool is opinion-based. Reflect on all the rules it imposes and whether they actually make sense in your specific project or not. It's also not the only "lint" tool for JavaScript. Maybe ESLint or another such tool is more suited to your needs.

But I also disagree with him: It is not good practice to declare all your variables at the start of your function, especially not if the function is rather long, since this makes prehension of your program much harder. IMHO this holds regardless of how the scoping of JavaScript works: It's not about program semantics, it's about code readability!

I would argue that a variable should be declared as close to its first use as possible (i.e. in your case: in the for loop). This ensures that someone reading your code (a colleague or yourself three months from now) has too keep as little information in their head as possible. Declaring all your variables at the start forces the reader to keep all those variables in mind throughout the entire function. Questions like "what's the current value of this variable?" or "what is its purpose?" bee harder to answer.

Furthermore, you are much more tempted to reuse a variable for more than one purpose. This is not only confusing, but also dangerous! Values might "leak" from the first use to the second. This can lead to subtle, hard-to-debug problems.

My personal opinion is that JSLint is retarded. But opinions may vary on that sensitive topic.
— dystroy, 45 secs ago

Hey, that's actually a valid point. JSLint is entirely constructed on opinion, and it not the word of god.

However, general good practice is to declare all your variables in one place, at the start of the function block they are in:

function doSomething() {
    var a, b, c, d;
    a = 1;
    c = 10;
    for( b = a; b < c; b++) {
        d = b*2 + a-c;
        alert(d);
    }
}

Because creating a bar in the for loop creates a global var. it's one of those things that JavaScript does and most people don't realize. And if you don't and you or someone else creates that same var in the scope of that function or global scope then that my friend would be one of the famous JavaScript foot guns.

本文标签: javascriptWhy does not JSLint allow quotvarquot in a for loopStack Overflow