Last active
April 15, 2024 10:50
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
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");
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
Do not access Object.prototype method 'hasOwnProperty' from target object. It is a 'no-prototype-builtins' error.