अब मैं एक एप्लिकेशन विकसित कर रहा हूं, और एक वैश्विक 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.');
फिर मुझे यह परिणाम फ़ायरफ़ॉक्स कंसोल में मिलता है।
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।