这属于代码级别的修改,重写console.log(),下面贴代码,有详细的注释:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
<!DOCTYPE html><html> <head> <meta charset="{CHARSET}"> <title></title> <script> var Debugger = function () { }; //开关,是否显示输出 Debugger.switch = true; Debugger.log = function (message){ try{ if(Debugger.switch){ console.log(message); } }catch(exception){ return 'Error:the function log is not exist.'; } } var name = '音乐586'; //会在控制台输出'音乐586' Debugger.log(name); //如果你不想有输出,把开关关了即可 Debugger.switch = false; Debugger.log(name);//控制台不再输出name </script> </head> <body> </body></html>还有一种方法是直接将console.log的功能去掉,js开头写console.log=function(){};算是一个小技巧。。 |
本文介绍了一种在JavaScript中重写console.log()的方法,通过创建Debugger类来控制日志输出,实现按需开关日志的功能,并提供了一个简单的示例。
1519

被折叠的 条评论
为什么被折叠?



