Skip to content

Instantly share code, notes, and snippets.

@LukeberryPi
Last active April 15, 2024 10:50
Show Gist options
  • Save LukeberryPi/ceacb1423ae0449b7c1ad8cbc9d8ad2e to your computer and use it in GitHub Desktop.
Save LukeberryPi/ceacb1423ae0449b7c1ad8cbc9d8ad2e to your computer and use it in GitHub Desktop.
Do not access Object.prototype method 'hasOwnProperty' from target object. It is a 'no-prototype-builtins' error.
// if you have a javascript object and want to check if it has a key (property)
const obj = {
"a": 1,
"b": 2,
"c": 3,
}
// checking directly from the object might piss off ESLint
obj.hasOwnProperty("c"); // 'no-prototype-builtins' error.
// to make the error go away, do it like this
Object.prototype.hasOwnProperty.call(obj, "c"); // true
// ESLint is trying to prevent you from possible shadowing of the hasOwnProperty method
// further explanation: https://stackoverflow.com/a/39283005
@yo-han
Copy link

yo-han commented Apr 15, 2024

As this solution is high in the google results I suggest a little correction here. The solution is almost correct but gives an error. The correct way is:

Object.prototype.hasOwnProperty.call(obj, "c");

@LukeberryPi
Copy link
Author

You are correct, I will update it. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment