I'm trying to check if document has 'hidden' property using document.hasOwnProperty but it always returns false in Chrome (74).
I've tried Object.prototype.hasOwnProperty but that too returns false. When I tried to stringify and parse back document I got back Location object as a property.
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? 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
.
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.
(thanks to @Jonas Wilms for clarification)