28

This seems quite bizarre.

Here's my experiment in the IE8 console:

typeof obj1 // "object"
obj1.hasOwnProperty // {...}

typeof obj2 // "object"
obj2.hasOwnProperty // undefined

Any ideas as to what could cause this?

Gezim
  • 7,112
  • 10
  • 62
  • 98
  • is obj2 a host object? Are you in IE7/ IE8 / quirks mode ? – Raynos Nov 16 '11 at 19:48
  • 1
    regarding `difference between native objects and host objects?`: http://stackoverflow.com/questions/7614317/what-is-the-difference-between-native-objects-and-host-objects – Adriano Oct 14 '14 at 10:03
  • related http://stackoverflow.com/questions/135448/how-do-i-check-to-see-if-an-object-has-a-property-in-javascript – Adriano Oct 15 '14 at 07:34
  • A workaround that doesn't need the use of hasOwnProperty(): [hasOwnProperty() is undefined on the window object in IE8 and causes a TypeError](http://sixdayprogrammer.com/2014/10/138/) – Chris Bier Oct 28 '14 at 18:46
  • I had this problem as I wanted to know if there is a property exists in object or not so , I can solve by this Link : http://stackoverflow.com/questions/11040472/check-if-object-property-exists-using-a-variable – Harry Sarshogh Mar 29 '15 at 05:58

1 Answers1

37

This example is from IE8, but the same return is from IE6+ and most other IE browsers.

IE before #9 does not define it for host objects

var o=window;// or document or document elements
o.hasOwnProperty

/*  returned value: (undefined)
undefined
*/
kennebec
  • 102,654
  • 32
  • 106
  • 127
  • 40
    Maybe `Object.prototype.hasOwnProperty.call(window,name)`? – panzi Apr 11 '12 at 18:24
  • 2
    @panzi: Thank you so much! This works in IE8, and now incompatible browsers are properly detected my my site. (before, would crash on blank page, since no window.hasOwnProperty) – Andrea Nov 07 '12 at 13:10
  • 2
    @panzi: It would be probably better if you post the information from comment as the answer. It's really *the solution* of the problem. – Oleg Mar 05 '15 at 15:13
  • 1
    @Oleg It was only guessed (hence the "maybe"). Does it actually work? Then I'll write an answer. – panzi Mar 05 '15 at 19:42
  • @panzi: As I know all DOM elements/nodes created in IE8 have the same problem with `hasOwnProperty`, but one can successfully use `Object.prototype.hasOwnProperty.call(domElement,name)`. – Oleg Mar 07 '15 at 10:21