简体   繁体   English

document.hasOwnProperty(“ hidden”)返回false,但是文档具有隐藏的属性

[英]document.hasOwnProperty(“hidden”) returns false but document has the property hidden

I'm trying to check if document has 'hidden' property using document.hasOwnProperty but it always returns false in Chrome (74). 我正在尝试使用document.hasOwnProperty检查文档是否具有“隐藏”属性,但它在Chrome(74)中始终返回false。

I've tried Object.prototype.hasOwnProperty but that too returns false. 我已经尝试过Object.prototype.hasOwnProperty,但这也返回false。 When I tried to stringify and parse back document I got back Location object as a property. 当我尝试对文档进行字符串化和解析时,我得到了Location对象作为属性。

 console.log(document.hasOwnProperty("hidden")); console.log(Object.prototype.hasOwnProperty.call(document, "false")); console.log(JSON.parse(JSON.stringify(document))); console.log(typeof document.hidden !== "undefined"); console.log(document.hidden); console.log(Document.prototype.hasOwnProperty.call(document, "hidden")); console.log(Document.prototype.hasOwnProperty.call(document, "location")); 

Shouldn't hasOwnProperty check if an object has a property irrespective of the object type? hasOwnProperty是否应该检查对象是否具有属性,而与对象类型无关? I apologize if the question has already been answered. 如果问题已经得到解答,我深表歉意。

The purpose of hasOwnProperty() is to check whether a certain property is defined on the instance itself and is not inherited through its prototype . hasOwnProperty()的目的是检查某个属性是否在实例本身上定义,并且是否通过其prototype继承。

In the case of document , it rightfully returns false since the hidden property is actually defined on the Document interface and not on the instance itself. 对于document ,它正确返回false因为hidden属性实际上是在Document接口而不是实例本身上定义的。

(thanks to @Jonas Wilms for clarification) (感谢@Jonas Wilms的澄清)

Copying and correcting @haim770's deleted answer for now: 现在复制并更正@ haim770的已删除答案:

The purpose of hasOwnProperty() is to check whether a certain property is defined on the object itself and is not inherited through its prototype . hasOwnProperty()的目的是检查某个属性是否在对象本身上定义,并且是否通过其prototype继承。

In the case of document , it rightfully returns false since the hidden property is actually defined on [ Document ] and not on [the document object] itself. 对于document ,它正确地返回false因为hidden属性实际上是在[ Document ]上定义的,而不是在[document object]本身上定义的。

 console.log('' + Object.getPrototypeOf(document)); console.log('' + Object.getPrototypeOf(Object.getPrototypeOf(document))); console.log(document.__proto__.__proto__.hasOwnProperty('hidden')); console.log(Object.getOwnPropertyDescriptor(Document.prototype, 'hidden')); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么选择document.createElement('canvas')。getContext('2d')。 hasOwnProperty('font')返回false? - Why document.createElement('canvas').getContext('2d'). hasOwnProperty('font') returns false? 在WebBrowser控件中使用document.hidden错误地为false - document.hidden is incorrectly false when used in a WebBrowser control 溢出隐藏文档的滚动偏移 - Scroll Offset of Overflow Hidden Document 如何模拟文档,用 Jasmine 隐藏? - How to mock document,hidden with Jasmine? hasOwnProperty在FF中返回false - hasOwnProperty returns false in FF Javascript hasOwnProperty() 即使有属性也返回 false(Hackerrank 问题) - Javascript hasOwnProperty() return false even if it has property (Hackerrank Question) document.hidden时暂停/恢复setInterval - Pause/resume setInterval when document.hidden 为什么我们应该更喜欢 document.visibilityState 而不是 document.hidden? - Why are we supposed to prefer document.visibilityState to document.hidden? document.hidden 与 document.hasFocus() 之间的区别 - Difference between document.hidden vs document.hasFocus() 为什么 hasOwnProperty 在构造函数上返回 false,但在未实例化函数的情况下访问字符串属性时返回 true - Why hasOwnProperty returns false on a constructor function but string property returns true when accessed without instantiating the function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM