On a live website, I've previously changed console.log to be:
var console = {};
console.log = function () {};
With this code always executing, is it possible to reinstate the console functionality after this code, and if so, how?
It's needed, since the site is live. But I need to do further development, without making any updates to the live site. It's hard without console logging/information.
If that code is not running on the top level, because console
is on window
, you can just reference window.console.log
, eg:
(() => {
var console = {};
console.log = window.console.log;
console.log('foo');
// or, just reference window:
window.console.log('bar');
})();
If the code is running on the top level and you're using const
or let
(rather than ES5 var
), you can do the same thing:
const console = {};
console.log = window.console.log;
console.log('foo');
Otherwise, you'll have to save a reference to console.log
before you reassign console
with var
:
var log = console.log;
var console = {};
console.log = log;
log('foo');
But it's probably not a great idea to create/overwrite built-in global variables like that unless absolutely necessary - the IIFE in the first snippet is probably preferable.