[vue-cli]直接使用hasOwnProperty报错
学习vue中
出现报错
foo是一个对象,我想要判断它是否带有bar属性。
我原本的写法:
if(foo.hasOwnProperty('bar')){
//doSomething...
}
但是报错了:
error Do not access Object.prototype method 'hasOwnProperty' from target object no-prototype-builtins
报错原因
新版本ESlint禁止对象直接调用Object.prototype的方法。
ERROR in [eslint]
解决方法
1. 忽略报错(不推荐)
- 只要在
<script>标签内注释写上一句/* eslint-disable */,这个报错就会被忽略。
Use /* eslint-disable */ to ignore all warnings in a file.
2. 修改代码(推荐)
将foo.hasOwnProperty('bar')修改为Object.hasOwnProperty.call(foo,'bar')

浙公网安备 33010602011771号