【发布时间】:2009-07-18 00:38:22
【问题描述】:
我已经安装了 firebug 并编写了所有这些日志语句。
我已经在 IE 中测试了我的应用程序,当然我遇到了“未定义”错误。
避免这种情况的常用习语是什么。
我真的不想评论我文件中的所有 console.log 语句,也不想模拟它们。
嗯,我不知道该怎么办。
【问题讨论】:
标签: javascript console firebug undefined
我已经安装了 firebug 并编写了所有这些日志语句。
我已经在 IE 中测试了我的应用程序,当然我遇到了“未定义”错误。
避免这种情况的常用习语是什么。
我真的不想评论我文件中的所有 console.log 语句,也不想模拟它们。
嗯,我不知道该怎么办。
【问题讨论】:
标签: javascript console firebug undefined
我通常会像这样制作一个包装函数:
function log(obj) {
if (window.console && console.log) console.log(obj);
}
或者你可以在你的脚本文件/元素的开头做这样的事情:
if (!window.console) {
window.console = {
log: function(obj){ /* define own logging function here, or leave empty */ }
};
}
【讨论】:
Paul Irish 为 console.log() 提供了更好的包装器。
http://paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
这允许多个参数,并提供历史记录(用于调试)以防没有控制台或(例如 Firebug Lite)稍后创建控制台。
【讨论】: