Why does not JSLint allow “var” in a for loop?

前端 未结 3 661
离开以前
离开以前 2021-01-14 08:03

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 co

3条回答
  •  攒了一身酷
    2021-01-14 08:48

    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);
        }
    }
    

提交回复
热议问题