你推荐哪个解决方案,第二个更简单(代码更少),但使用它会有缺点?
第一个:(设置全局调试标志)
// 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);
}
第二:覆盖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);
this.console = ...
或者var console = ...
?如果按照你目前的写法,在 ECMAScript 5 严格模式下会报错。 - Tim Downwindow.console
最适合可移植性。 - Andy E