It’s well documented that native DOM elements in IE do not contain the hasOwnProperty() method. There are a couple of solutions to this; the most elegant of which involves accessing the hasOwnProperty() method directly in Object.prototype, like so:
Object.prototype.hasOwnProperty.call(element, name);
It seems to me that this no longer works in IE9. Can someone explain? Here’s a fiddle illustrating this.
Using IE9 in Standards mode with Compatibility Mode turned off, the alert displays:
sessionStorage is supported: false
localStorage is supported: false
However, after turning the Compatibility or Quirks Mode on (or using the Developer Tools to render the page using the IE8 and IE7 engines) the alert displays:
sessionStorage is supported: true
localStorage is supported: true
Is there something else at work here, or is this workaround for hasOwnProperty no longer usable?
It’s because those properties are stored in the prototype chain of
windowinstead of directly on the object.If you use
in, it’ll search the prototype chain for you, and give youtrueinIE9.http://jsfiddle.net/fHRZs/2/
So
.hasOwnProperty()is giving the correct result.