The body of a for-in should be wrapped in an if statement to filter unwanted properties from the pro

ESLint模式下for in遍历对象会报错,可以这样解决:

let val = { shu: [1, 2, 3] };
for (let item in val) {
  if (val.hasOwnProperty(item)) { 
    console.log(item);
  }
}

因为我们在遍历一个对象之前应该判断一下这个对象是否有自身的属性(非继承),如果这个对象是个空对象,那么就没有必要遍历了呗~,提升代码的效率。

posted @ 2021-04-26 08:51  浅笑19  阅读(1852)  评论(0编辑  收藏  举报