[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號