How to safely wrap `console.log`?

后端 未结 10 510
日久生厌
日久生厌 2021-01-31 14:13

Suppose I want to include some calls to console.log for some legitimate production reason, say for something like a unit test harness. Obviously I would not want th

10条回答
  •  梦如初夏
    2021-01-31 15:06

    My solution is a little different. I create a standard shortcut for all console.log calls: In my case kag(whatever I want to report in the console).

    I test for IE, if IE I send the results to an alert box. If Chrome then displays in the console. This also means IE will always work even in console is closed:

    Code:

    var iever = getInternetExplorerVersion();
    if(iever>-1){
    function kag(params){
        alert(params);
    }
    } else {
    var kag = console.log.bind(console, "REPORT: ");
    }
    
    function getInternetExplorerVersion() {
    var rv = -1;
    if (navigator.appName == 'Microsoft Internet Explorer') {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null){
      rv = parseFloat( RegExp.$1 );
    }
    }
    return rv;
    }
    

提交回复
热议问题