object has no hasOwnProperty method (i.e. it's undefined) - IE8
Asked Answered
A

1

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?

Aerodrome answered 16/11, 2011 at 19:46 Comment(5)
is obj2 a host object? Are you in IE7/ IE8 / quirks mode ?Aspersorium
regarding difference between native objects and host objects?: #7614817Rideout
related #135948Rideout
A workaround that doesn't need the use of hasOwnProperty(): hasOwnProperty() is undefined on the window object in IE8 and causes a TypeErrorChallis
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 : #11040972Repressive
R
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
*/
Redneck answered 16/11, 2011 at 22:9 Comment(5)
Maybe Object.prototype.hasOwnProperty.call(window,name)?Llewellyn
@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)Prudenceprudent
@panzi: It would be probably better if you post the information from comment as the answer. It's really the solution of the problem.Deon
@Deon It was only guessed (hence the "maybe"). Does it actually work? Then I'll write an answer.Llewellyn
@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).Deon

© 2022 - 2024 — McMap. All rights reserved.