[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')

posted @ 2022-07-30 15:53  feixianxing  阅读(504)  评论(0编辑  收藏  举报