Based on hasOwnProperty()
method docs I wrote the following:
const myObj = {
prop1: 'val1',
prop2: 'val2'
}
if (!myObj.hasOwnProperty('prop3')) {
myObj.prop3 = 'val3'
}
But I'm getting this error:
Do not access Object.prototype method 'hasOwnProperty' from target object
Why does it not work if it's the same as in the docs, and how to fix it?
Object.prototype.hasOwnProperty.call(obj, prop)
did the job yes. But that doesn't explain why ESlint refuses a code copied straight from MDN docs – Raffo