0

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.

shrys
  • 5,860
  • 2
  • 21
  • 36

2 Answers2

2

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)

haim770
  • 48,394
  • 7
  • 105
  • 133
  • No, it is not. It is defined on the [Document interface.](https://developer.mozilla.org/en-US/docs/Web/API/Document) – Jonas Wilms May 29 '19 at 11:18
  • Why did you delete this instead of updating it to say that the property is on `Document`? – Ry- May 29 '19 at 11:31
  • @Ry-, Indeed, I was too quick to delete. – haim770 May 29 '19 at 11:59
  • Thank you for the input @haim770. I would like to ask if there's a way to check the property exists with Document, as I've tried and still get false for ```hidden``` – shrys May 29 '19 at 12:13
  • @shrys, What exactly are you trying to achieve? What's wrong with `typeof document.hidden !== "undefined"`? – haim770 May 29 '19 at 13:14
  • I could do that or as @Ry- suggested I could use ```typeof Object.getOwnPropertyDescriptor(Document.prototype, 'hidden') == "object"```. I actually wanted to keep it generic for other browsers as well which have a different name for ```hidden``` – shrys May 29 '19 at 13:19
  • @shrys, Did you check https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API#Example? – haim770 May 29 '19 at 13:24
  • @haim770 yes I did see that. I guess I should go with this implementation. Thanks. – shrys May 29 '19 at 13:35
1

Copying and correcting @haim770’s deleted answer for now:

The purpose of hasOwnProperty() is to check whether a certain property is defined on the object 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 [Document] and not on [the document object] itself.

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'));
Community
  • 1
  • 1
Ry-
  • 218,210
  • 55
  • 464
  • 476