最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is there a way to silence JSHint's "variable is already defined" warning? - Stack Overflo

programmeradmin2浏览0评论

JSHint complains if I have multiple for loops declaring the same index variable:

for(var i=0; i<10; i++){
    console.log(i);
}

for(var i=0; i<10; i++){   //<-- jshint warns that 'i' is already defined
    console.log(i);
}

Is there a way to turn this warning off? I couldn't find any when I searched...

The reason I want to do this is that I prefer keeping my index variables declared together with the loops instead of hoisting the declarations to the top of the function. I think repeating the declarations is more robust if I delete the for loops or move them around and I also think it helps convey the intention that the loop variables should not be used outside the loops.

JSHint complains if I have multiple for loops declaring the same index variable:

for(var i=0; i<10; i++){
    console.log(i);
}

for(var i=0; i<10; i++){   //<-- jshint warns that 'i' is already defined
    console.log(i);
}

Is there a way to turn this warning off? I couldn't find any when I searched...

The reason I want to do this is that I prefer keeping my index variables declared together with the loops instead of hoisting the declarations to the top of the function. I think repeating the declarations is more robust if I delete the for loops or move them around and I also think it helps convey the intention that the loop variables should not be used outside the loops.

Share Improve this question edited Aug 3, 2014 at 15:48 hugomg asked Aug 3, 2014 at 6:31 hugomghugomg 69.9k29 gold badges164 silver badges254 bronze badges 8
  • 2 "I think repeating the declarations is more robust if I delete the for loops or move them around" - why? it's exactly that it will break miserably if you e. g. move one loop inside another because of the lack of block scope. – The Paramagnetic Croissant Commented Aug 3, 2014 at 6:32
  • I was thinking about when I move the loops laterally (this can be a problem if I leave the declaration in the first loop and remove it in the second one). – hugomg Commented Aug 3, 2014 at 6:35
  • Additionally, if I hoist the var i to the top of the function like the warning is suggesting me then jshint will let me nest for(i= loops inside one another without giving any warnings either. – hugomg Commented Aug 3, 2014 at 6:42
  • by not refining the variable? lol – AdityaParab Commented Aug 3, 2014 at 7:05
  • You could use a different letter for each for loop within a function. Then you'd silence jsHint and be free to move the loops wherever you want in that function without breaking anything, including even nesting them inside another loop. Note that ES6 intends to solve this with let used in a for loop initializer. – jfriend00 Commented Aug 3, 2014 at 7:29
 |  Show 3 more comments

1 Answer 1

Reset to default 20

The shadow option disables this warning.

/* jshint shadow:true */

for(var i=0; i<10; i++){ console.log(i); }
for(var i=0; i<10; i++){ console.log(i); }
发布评论

评论列表(0)

  1. 暂无评论