【问题标题】:How to best safely use console.log and console.warn?如何最安全地使用 console.log 和 console.warn?
【发布时间】:2016-01-20 17:15:19
【问题描述】:

我正在努力接管一个测试代码库,以确保 console.warn 存在如下:

if (window.console) {                                                                                                                                                                                                                                                      
  console.warn("shhaabang");                                                                                                                                                                                                    
}                                                                                                                                                                                                                                                                          

但是,我的问题是它仅适用于您拥有窗口对象的浏览器。这似乎也行得通。

if (console && typeof console.warn == 'function') {                                                                                                                                                                                                                                                      
  console.warn("shhaabang");                                                                                                                                                                                                    
}

这种方法有什么缺点吗?有没有更好的方法不假设window 的存在?我是否应该先查看是否存在 window 或 global 并检查两者?

澄清至少应该很明显,我在没有window 对象的东西上测试它。但是,明确地说 我在 node.js 以及浏览器中运行它

【问题讨论】:

    标签: javascript cross-browser cross-platform ecmascript-5 console.log


    【解决方案1】:

    考虑使用已经解决此问题的 javascript 日志记录框架,而不是重新发明轮子。例如,试试loglevel。

    以下是 loglevel 如何处理安全日志记录 (view full source):

    function realMethod(methodName) {
        if (typeof console === undefinedType) {
            return false; // We can't build a real method without a console to log to
        } else if (console[methodName] !== undefined) {
            return bindMethod(console, methodName);
        } else if (console.log !== undefined) {
            return bindMethod(console, 'log');
        } else {
            return noop;
        }
    }
    
    function bindMethod(obj, methodName) {
        var method = obj[methodName];
        if (typeof method.bind === 'function') {
            return method.bind(obj);
        } else {
            try {
                return Function.prototype.bind.call(method, obj);
            } catch (e) {
                // Missing bind shim or IE8 + Modernizr, fallback to wrapping
                return function() {
                    return Function.prototype.apply.apply(method, [obj, arguments]);
                };
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      你不应该这样做

      if(console && typeof console.warn == "function"){ ... }
      

      因为如果未定义控制台会引发错误

      你可以使用this

      例如:

      if(this.console){
        console.warn("shhaabang");
      }
      

      编辑:哦,抱歉,我只注意到两件事: 1) 这只有在this 没有被更改时才有效......谢谢@Evan Carroll 2) 这在strict mode 中不起作用,因为在strict mode 中,this 是undefined

      所以这是另一种方法:

      var c;
      try {
        c = console;
      } catch(e){ 
        // console is not defined
      }
      
      if(c) {
        // console is defined
      }else{
        // this will not throw because you declared `c` earlier
      }
      

      【讨论】:

        猜你喜欢
        • 2011-06-29
        • 2016-09-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多