[英]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 itsprototype
.hasOwnProperty()
的目的是检查某个属性是否在对象本身上定义,并且是否通过其prototype
继承。In the case of
document
, it rightfully returnsfalse
since thehidden
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.