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

后端 未结 8 872
长情又很酷
长情又很酷 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: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]);
      }
    }
    

提交回复
热议问题