How to quickly and conveniently disable all console.log statements in my code?

后端 未结 28 2510
深忆病人
深忆病人 2020-11-22 16:48

Is there any way to turn off all console.log statements in my JavaScript code, for testing purposes?

28条回答
  •  忘了有多久
    2020-11-22 17:39

    I am surprised that of all those answers no one combines:

    • No jquery
    • Anonymous function to not pollute global namespace
    • Handle case where window.console not defined
    • Just modify the .log function of the console

    I'd go for this:

    (function () {
    
        var debug = false
    
        if (debug === false) {
            if ( typeof(window.console) === 'undefined') { window.console = {}; }
            window.console.log = function () {};
        }
    })()
    

提交回复
热议问题