题
我正在编写一个快速日志函数来包装本机 console.log
,以防止以后在开发中出现错误(比如代码中遗忘的 console.log
语句)。我正在使用mootools $ defined
和 $ type
用于检查是否存在调用它之前的控制台和console.log函数。但是,当我禁用firebug时,我在firefox中收到以下错误。
错误:未定义控制台
源文件: http://diagnostic.localhost/js/emp.custom.js 点击 行:6
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);
}
}
似乎使用 $ defined
应该可以消除此错误,所以有人对这个问题有什么想法吗?
我正在使用mootools v1.2.3。
编辑:我尝试了以下内容,他们也给了他们同样的错误:if (DEBUG && $type(console) == "object" && $type(console.log) == 'function') {
if (DEBUG && $chk(console) && $type(console.log) == 'function') {
解决方案
请尝试 $ defined(window.console)
。如果Firefox通过作用域链并且找不到变量,那么它会抛出一个错误,但如果您明确了要查找变量的上下文,它会给你 undefined 代码>
不隶属于 StackOverflow