Access window.console after overwrite

前端 未结 4 1540
醉话见心
醉话见心 2020-12-10 14:15

Is it possible to somehow access to console.log after it gets overwritten?

window.console = { log: function (msg) { alert(msg); }, /* etc... */ };

4条回答
  •  暖寄归人
    2020-12-10 14:58

    Edit (2017-04-08): This advise is outdated, in Firefox 52 and Chrome 57 console is no longer defined on the window prototype and deleting it will really delete it.


    At least with the console object defined by Firefox and Chrome, you can simply delete the overwritten property to restore the original one:

    window.console = {};
    delete window.console;
    window.console.log("This works!");
    

    This works as if the console property were defined on the prototype of the window object - except that it isn't, the browsers are doing some magic here.

提交回复
热议问题