代码之家  ›  专栏  ›  技术社区  ›  TJ L

当我专门检查控制台是否存在时,控制台未定义

  •  1
  • TJ L  · 技术社区  · 16 年前

    我正在编写一个快速日志函数来包裹本地 console.log 防止开发后期出现错误(如遗忘 console.log $defined 和 $type

    错误:未定义控制台
    源文件: http://diagnostic.localhost/js/emp.custom.js

    EMP.log = function() {
     if (DEBUG && $defined(console) && $type(console.log) == 'function') { //line 6
      var args = Array.prototype.slice.call(arguments); //turn arguments into array
      console.log.pass(args);
     }
    }
    

    应该消除这个错误,那么有人知道问题可能是什么吗?

    if (DEBUG && $type(console) == "object" && $type(console.log) == 'function') {
    
    if (DEBUG && $chk(console) && $type(console.log) == 'function') {
    
    1 回复  |  直到 16 年前
        1
  •  5
  •   Tmdean    16 年前

    尝试 $defined(window.console) 相反。如果Firefox在作用域链中找不到变量,它会抛出错误,但如果你在查找变量的上下文中是显式的,它会给你 undefined .