前言
在判断一个对象中是否有一个属性时,我调用了Object对象上的hasOwnProperty()接口时,控制台报错报错信息如下:
Do not access Object.prototype method 'hasOwnProperty' from target object no-prototype-builtins
,把此错误信息翻译过来是:目标对象没有Object原型对象上的方法hasOwnProperty的使用权,规则是no-prototype-builtins;意思就是no-prototype-builtins规则限制了我们在目标对象上通过原型链使用Object上的方法;下面我将为大家提供解决办法;
方法一
let events = {"some-index": false};
let key = "some-index";
if(Object.prototype.hasOwnProperty.call(events, key)) {
// This would compile without any issue !
console.log("The object has the property");
}
此方法很显然绝对不会出现问题,直接使用Object.prototype.hasOwnProperty.call(events, key)调用接口,然后用call方法改变this指向,将this指向目标对象,传入要判断的参数key就将问题解决;
方法二
let events = {"some-index": false};
let key = "some-index";
if(!!Object.getOwnPropertyDescriptor(events, key)) {
// This would compile without any issue !
console.log("The object has the property");
}
此方法是直接调用了getOwnPropertyDescriptor(events, key)这个方法也能解决问题;
方法三
最后一种是最暴力的方法,直接将ESlint的配置项lintOnSave改为false重新启动以下VSCode也能解决问题
const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
transpileDependencies: true,
lintOnSave: false,
})
写在最后
🥂(❁´◡`❁)您的点赞👍➕评论📝➕收藏⭐是作者创作的最大动力🤞