本文介紹了如果未定義,js 會覆蓋 console.log的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
您推薦哪種解決方案,第二種更簡單(代碼更少),但使用它有缺點?
Which solution do you recommend, the second is simpler ( less code ), but there are drawbacks on using it ?
首先:(設置全局調試標志)
// 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
Second: override 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);
推薦答案
不是,而是第二種的變體.丟失 try...catch
并正確檢查控制臺對象是否存在:
Neither, but a variation of the second. Lose the try...catch
and check for existence of the console object properly:
if (typeof console == "undefined") {
window.console = {
log: function () {}
};
}
console.log("whatever");
這篇關于如果未定義,js 會覆蓋 console.log的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!