Rad*_*ris 27 javascript console firebug
你推荐哪种解决方案,第二种更简单(代码更少),但使用它有缺点吗?
第一:(设置全局调试标志)
// the first line of code
var debug = true;
try {
console.log
} catch(e) {
if(e) {
debug=false;
}
};
// Then later in the code
if(debug) {
console.log(something);
}
Run Code Online (Sandbox Code Playgroud)
第二: 覆盖console.log
try {
console.log
} catch(e) {
if (e) {
console.log = function() {}
}
};
// And all you need to do in the code is
console.log(something);
Run Code Online (Sandbox Code Playgroud)
And*_*y E 56
两者都不是,而是第二种的变种.丢失try...catch
并正确检查控制台对象是否存在:
if (typeof console == "undefined") {
window.console = {
log: function () {}
};
}
console.log("whatever");
Run Code Online (Sandbox Code Playgroud)
或者,在coffeescript中:
window.console ?=
log:-> #patch so console.log() never causes error even in IE.
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
28929 次 |
最近记录: |