Reputation: 90101
https://eslint.org/docs/rules/no-prototype-builtins and https://stackoverflow.com/a/12017703/14731 imply that:
Object.prototype.hasOwnProperty.call(foo, "bar")
is equivalent to (and safer than) foo.hasOwnProperty("bar")
However when running Chrome 75.0.3770.142 I see:
Number.prototype.hasOwnProperty("toString") // true
Object.prototype.hasOwnProperty.call(Number, "toString") // false
Questions:
Upvotes: 1
Views: 1551
Reputation: 29086
You are actually checking two different things:
Number.prototype.hasOwnProperty("toString")
will check if the prototype of the Number
object contains the toString
property and it doesn't come from its prototype chain (hasOwnProperty).Object.prototype.hasOwnProperty.call(Number, "toString")
is essentially the same as Number.hasOwnProperty("toString")
- it's not checking the prototype but the Number
object itself.These are two different objects as can be seen here:
console.log(Number === Number.prototype)
So, the prototype is what contains the toString
property and Number
inherits it through that. So, in order to have equivalent check you need to check the prototype:
console.log(Number.prototype.hasOwnProperty("toString")); // = true
console.log(Object.prototype.hasOwnProperty.call(Number.prototype, "toString")); // = true
console.log(Number.hasOwnProperty("toString")); // = false - not on this object...
console.log("toString" in Number); // = true - ...so it it's inherited
Upvotes: 2