What does the JSLint error 'body of a for in should be wrapped in an if statement' mean?

后端 未结 8 858
长情又很酷
长情又很酷 2020-11-30 15:57

I used JSLint on a JavaScript file of mine. It threw the error:

for( ind in evtListeners ) {

Problem at line 41 character 9:

相关标签:
8条回答
  • 2020-11-30 16:40

    This means that you should filter the properties of evtListeners with the hasOwnProperty method.

    0 讨论(0)
  • 2020-11-30 16:53

    Bad: (jsHint will throw a error)

    for (var name in item) {
        console.log(item[name]);
    }
    

    Good:

    for (var name in item) {
      if (item.hasOwnProperty(name)) {
        console.log(item[name]);
      }
    }
    
    0 讨论(0)
提交回复
热议问题