ตอนนี้ฉันกำลังพัฒนาแอปพลิเคชันและวาง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ผ่านฟังก์ชั่น wrapper