简体   繁体   English

JSHint认为for-in变量'bad'。这是什么意思?

[英]JSHint considers a for-in variable 'bad'. What does this mean?

The following code: 以下代码:

var things = {'foo':'bar'}
for ( thing in things ) {
  console.log(thing)
}

Consistently produces the following error in jshint: 在jshint中始终产生以下错误:

Bad for in variable 'thing'.

I do not understand what makes the 'thing' variable 'bad' - as you can see, it is not being used anywhere else. 我不明白是什么让“东西”变量“变坏” - 正如你所看到的,它并没有被其他任何地方使用。 What should I do differently to make jshint not consider this to be an error? 我应该做些什么来使jshint不认为这是一个错误?

They always are - if they are not declared. 他们总是 - 如果他们没有宣布。 Try adding var if thing has not been previously declared. 尝试添加var如果thing以前尚未宣布。

for ( var thing in things ) {
  console.log(thing)
}

or 要么

var thing;

//more code

for ( thing in things ) {
  console.log(thing)
}

Here is your code slightly modified, make sure all is declared before usage. 这是您的代码稍作修改,确保在使用前声明所有代码。

var things = {'foo':'bar'}, thing;
for ( thing in things ) {
  console.log(thing)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM