0

I wonder why one and the same entity has a different representation inside browser window object called in console:

  1. If we call just window inside browser console we'll have a whole browser info. Where the document will be represented as object-liked entity included various props and functions (including DOM).
  2. If we call document directly by window.document command we'll have just only the DOM representation of it.

So, why does it happen? I really cannot figure out.

Thanks.

Max Travis
  • 1,228
  • 4
  • 18
  • 41
  • Possible duplicate of [What is the difference between window, screen, and document in Javascript?](https://stackoverflow.com/questions/9895202/what-is-the-difference-between-window-screen-and-document-in-javascript) – devlin carnate Jan 03 '19 at 21:46
  • 1
    No, it's a fully different question. @devlincarnate – Max Travis Jan 03 '19 at 21:47
  • @devlincarnate this is a different question. Delete your duplication mark in my post! I'm not asking about dimensions of the DOM, instead I'm wonder about differents in document and window.document! – Max Travis Jan 03 '19 at 21:51
  • I don't know the proper answer but I assume it is just a matter of interpretation. Chrome developers just assumed that in console, when you type `document` you want to see the DOM, whereas if you want all info you just type `window.document`. – Oliver Tušla Jan 03 '19 at 21:54
  • @OliverTušla haha, just type the what you wrote above - `window.document` and you will be wonder by what you see in console.... You don't right – Max Travis Jan 03 '19 at 21:56
  • Sorry, meant typing `window` and seeing the property for yourself. – Oliver Tušla Jan 03 '19 at 21:58
  • There's this (exact) duplicate [javascript window.document in the console](https://stackoverflow.com/questions/47383223/javascript-window-document-in-the-console) (which has no accepted or upvoted answer and thus is not available as dupe target). It is closed as dupe of [What's the difference between console.dir and console.log?](https://stackoverflow.com/questions/11954152/whats-the-difference-between-console-dir-and-console-log) which uses `window.document` in one of its answers as showcase. – Andreas Jan 03 '19 at 22:02
  • if your app opens a new window.e.g childWindow=window.open(//some params); them you have window.document and childWindow.document – sferret Jan 03 '19 at 22:06

2 Answers2

2

This is just the way chrome's developer tools work. They have a few different formats that they can output information in the console. Dom nodes, which are a type of object, have gotten their own fancy implementation, since they're such a common occurrence in web development. So when you do window.document, that's the format it chooses to output it.

For other types of objects they output it in a different format, and that's what it's doing when you do window. It's true you can expand this to drill into window.document, but the dev tools keep displaying it in the same format, rather than trying to nest one format inside another.

Nicholas Tower
  • 72,740
  • 7
  • 86
  • 98
  • _"So when you do window.document, that's the format it chooses to output it."_ - Use `console.dir(window.document)` instead of `console.log()` – Andreas Jan 03 '19 at 22:05
  • @Andreas I don't think he's doing either of those, he's just typing `document` and `window.document` directly into the console. – Barmar Jan 03 '19 at 22:06
0

straight from my chrome debugger:

>window.document === document
true

The documentantion relays that window.document is just a reference to document.

However even if they refer to the same document, the document might contain special getters and setters which overide those of window.document...

Nuno Sousa
  • 832
  • 7
  • 15
  • Getters and setters can't tell what expression was used to access them. – Barmar Jan 03 '19 at 22:05
  • Yes, but windows getters and setters are run first before documents getters and setters, and they could potentially overide them. I can run you an example if you would like. – Nuno Sousa Jan 03 '19 at 22:15
  • I see what you're saying. Writing just `document` doesn't access it through the Window object, so it doesn't use the getter/setter. – Barmar Jan 03 '19 at 22:17
  • Yes, absolutely, it's just something that **could** happen, and which might explain the difference in output. I have absolutely no idea if it does. (probbly not) – Nuno Sousa Jan 03 '19 at 22:18