I use the console object in javascript for debugging, and would like to overwrite it as to make use of such functionality in mobile browsers.
However , I have trouble understanding the following MDN documentation
The Window.console read-only property returns a reference to the Console object, which provides methods for logging information to the browser's console.
(https://developer.mozilla.org/en-US/docs/Web/API/Window/console)
In particular:
Window interface, but it seems to be different to the global window propertyYou can't use window.console = { ... } because window.console is a read-only property ! – Luillyfe Feb 23 at 21:28
What is it with the read-only-ness of Window.console or window.console ?
It's not read-only. MDN is an excellent resource, but it's edited by the community and sometimes errors or unsourced claims sneak in.
- I cannot make head's or tails' of what actually is the
Windowinterface, but it seems to be different to the global window property
The interface Window defines the properties and methods available on the window object in browsers, which is available via the default window global variable. You can think of it as this: var window = new Window() though of course that's not literally true .
- ... What is it with the read-only-ness of
Window.consoleorwindow.console?
It's not read-only.
Theory: The Window interface doesn't mention console at all, but the console spec says that it's a namespace object exposed on window. The WebIDL specification says that namespace objects are, by default, writable, configurable, and non-enumerable. (Thank you sideshowbarker for pointing that out!)
Practice: It's writable on every browser I've tried it on (Chrome, Firefox, Safari, IE9, IE11, Edge). In all of them except IE11, console is an "own" property on the object window refers to, and assigning to it works (in both loose and strict modes). (IE11 is the odd duck: console is not an own property of window when devtools are closed, but its type is "object" and if you log String(console) it the result is "[object Console]". But when devtools is open, it's an own property of window and not read-only.)
I've updated the MDN page, removing the "read-only" part of that sentence.
I'm not saying that replacing console with something else is a good idea. :-) But note that SO's own Stack Snippets do exactly that if you have the "Show console" option checked (as it is by default).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With