我的代码中有大量console.logs,但如果我需要在网站上查看它们,我只需要查看它们/打开它们,所以我想要一种能够在浏览器中启用/禁用它们的方法通过设置几个片段来控制台,例如: adysis.debug('enable') 、 adysis.debug('disable') 但我怎样才能让它们触发代码中的 console.logs ,我不知道目前还不知道该怎么做?
听起来您想使用可以从浏览器控制台运行的
console.log
和 adysis.debug('enable')
等命令来控制代码中 adysis.debug('disable')
语句的可见性。为了实现这一点,您可以创建一个自定义调试实用程序,该实用程序包装 console.log 函数并根据您的命令切换其行为。
// Custom debugging utility
const adysis = {
debugEnabled: false,
debug: function(action) {
if (action === 'enable') {
this.debugEnabled = true;
console.log('Debugging enabled.');
} else if (action === 'disable') {
this.debugEnabled = false;
console.log('Debugging disabled.');
}
},
log: function(...args) {
if (this.debugEnabled) {
console.log(...args);
}
}
};
// Usage in your code
function someFunction() {
adysis.log('This will only be logged if debugging is enabled.');
// ... rest of your code ...
}
// Usage in the browser console
adysis.debug('enable'); // Enable debugging
someFunction(); // This log will appear in the console
adysis.debug('disable'); // Disable debugging
someFunction(); // This log will not appear in the console