【Vue报错】Object.prototype method ‘hasOwnProperty‘ from target object no-prototype-builtins

前言

在判断一个对象中是否有一个属性时,我调用了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,
})

写在最后

🥂(❁´◡`❁)您的点赞👍➕评论📝➕收藏⭐是作者创作的最大动力🤞

评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端小二哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值