Difference Between Object.prototype.hasownproperty.call And {}.hasownproperty.call.(eslint Rule Guard-for-in)
In the eslint rule guard-for-in , use for in directly is incorrect. The good practice is for (key in foo) { if (Object.prototype.hasOwnProperty.call(foo, key)) { doSom
Solution 1:
They'll never lead to different results, which is why both of them are considered correct.
The object {} always inherits from the object prototype and has no own keys, so accessing the .hasOwnProperty method on it will get you what you want. And Object.prototype is a non-writable property of the global Object constructor, so it always refers to the object prototype as well.
Now, there are some cases where the expression does not do what you wanted it to do:
- Someone did overwrite the global
Objectvariable - Someone did shadow the
Objectidentifier in your scope so that it doesn't refer to the global - Someone did overwrite
Object.prototype.hasOwnPropertywith something else - Someone did overwrite
Function.prototype.callwith something else
1: Obviously, the first two edge cases only mess with the Object.prototype reference, but they're still edgy enough to be ignored.
Post a Comment for "Difference Between Object.prototype.hasownproperty.call And {}.hasownproperty.call.(eslint Rule Guard-for-in)"