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

时间:2021-04-27 14:25:28   收藏:0   阅读:0

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

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

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

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!