I am debugging a large JavaScript code base where, at some point, the "console" variable gets nulled when refreshing the page.
Is there a way to set a watch on console and make JavaScript break execution when that value changes (or when a condition (console == null)
is true)?
I am using Chrome on Windows 7.
You can't touch the
console
object... never, ever. The only thing that can happen is that aconsole
variable is declared in a scope/namespace, other than the global scope, hiding the global console. You can still access it usingwindow.console
, though. Other than that, the only things I can think of that cause this are:console
To find out where you need to look in the code set conditional breakpoints and watch a couple of expressions, and use the pause on uncaught exceptions button
The answer below doesn't work for
window.console
becauseconsole
(like other browser-native environment variables) is treated specially. Any attempt to assign a value toconsole
only "covers up" the original value; it does not replace it. You can't detect when theconsole
value changes, but you candelete window.console
to restore the original environment-supplied value.For other values, use
Object.defineProperty
to define a custom setter for some globalwindow.foobar
. The setter function runs wheneverwindow.foobar
is assigned a new value:Then, put a breakpoint in that setter function.
This approach will work for global variables or any object property (simply change
window
to the object that has the property).Browser-implemented functions can't be null-ed! Technically speaking.
If
window.console.log
funcion was assignednull
, then just restore it deleting it!That will do the job :)
EDIT: That's not an answer to your main question, but I think your question is comming from you searching a way to debug, so this answer basically skips the need to detect var changes.