26

這似乎很奇怪。object has no hasOwnProperty method(ie it undefined) - IE8

這裏是我的實驗在IE8控制檯:

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

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

任何想法,這是什麼原因?

+0

是OBJ2一個宿主對象?你在IE7/IE8 /怪癖模式? – Raynos

+1

關於'本地對象和宿主對象之間的區別?':http://stackoverflow.com/questions/7614317/what-is-the-difference-between-native-objects-and-host-objects –

+0

相關http:// stackoverflow.com/questions/135448/how-do-i-check-to-see-if-an-object-has-a-property-in-javascript –

回答

35

本例來自IE8,但IE6 +和大多數其他IE瀏覽器的返回值相同。

IE之前#9不限定它主機對象

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

/* returned value: (undefined) 
undefined 
*/ 
+0

謝謝你的答案。你知道解決方法嗎? – Phil

+38

也許'Object.prototype.hasOwnProperty.call(window,name)'? – panzi

+2

@panzi:非常感謝!這適用於IE8,現在不兼容的瀏覽器可以正確檢測到我的網站。 (之前,會在空白頁面上崩潰,因爲沒有window.hasOwnProperty) – Andrea

相關問題