我现在正在开发应用程序,并进行全局isDebug切换。我想包装起来console.log以方便使用。
//isDebug controls the entire site.
var isDebug = true;
//debug.js
function debug(msg, level){
var Global = this;
if(!(Global.isDebug && Global.console && Global.console.log)){
return;
}
level = level||'info';
Global.console.log(level + ': '+ msg);
}
//main.js
debug('Here is a msg.');
然后,我在Firefox控制台中得到此结果。
info: Here is a msg. debug.js (line 8)
如果我想使用debug()被呼叫的行号登录该info: Here is a msg. main.js (line 2)怎么办?
console需要使用。为了实现这一目标,包装器似乎是唯一的方法?

console.log信息,console.warn警告和console.error错误,而不用console.log通过包装器函数添加某些内容。