Categories
JavaScript Answers

How to fix Object.hasOwnProperty() yielding the ESLint ‘no-prototype-builtins’ error with JavaScript?

Spread the love

Sometimes, we want to fix Object.hasOwnProperty() yielding the ESLint ‘no-prototype-builtins’ error with JavaScript.

In this article, we’ll look at how to fix Object.hasOwnProperty() yielding the ESLint ‘no-prototype-builtins’ error with JavaScript.

How to fix Object.hasOwnProperty() yielding the ESLint ‘no-prototype-builtins’ error with JavaScript?

To fix Object.hasOwnProperty() yielding the ESLint ‘no-prototype-builtins’ error with JavaScript, we can use Object.prototype.hasOwnProperty.call instead of the hasOwnProperty of an object.

For instance, we write

Object.prototype.hasOwnProperty.call(obj, prop);

to call hasOwnProperty on obj with obj as the value of this.

We pass in prop as the 2nd argument to check if prop exists in obj.

Conclusion

To fix Object.hasOwnProperty() yielding the ESLint ‘no-prototype-builtins’ error with JavaScript, we can use Object.prototype.hasOwnProperty.call instead of the hasOwnProperty of an object.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *