eslint相关报错

本文介绍了如何通过ESLint配置修复代码规范问题,包括禁止函数参数前的空格,避免使用一元操作符`++`,以及在for-in循环中过滤原型属性。同时,提倡使用Object的方法替代for-in遍历,以防止遍历原型链。遵循这些最佳实践,可以提升代码质量和可读性。
摘要由CSDN通过智能技术生成

1、Unexpected space before function parentheses

问题:函数参数前不希望有空格

解决:.eslintrc文件中,加入配置

rules: {
    'space-before-function-paren': ['error', {
      anonymous: 'always',
      named: 'always',
      asyncArrow:'always'
    }],
  },

2、Unary operator ‘++‘ used

解决:将i++替换为i+=1

3、The body of a for-in should be wrapped in an if statement to filter unwanted properties from the prototype。

问题:避免for-in遍历原型属性

解决:加if判断

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

4、for..in loops iterate over the entire prototype chain, which is virtually never what you want. Use Object.{keys,values,entries}, and iterate over the resulting array.

问题:避免for-in遍历原型链

解决:使用Object.{keys,values,entries}

5、Trailing spaces not allowed

解决:删除标签后的空格

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值