当前位置:网站首页>你不知道的 console.log 替代品
你不知道的 console.log 替代品
2022-08-11 03:17:00 【幸福的达哥】
你不知道的 console.log 替代品
现在,假设您正在编写一些JS代码,但它无法正常工作。你做的第一件事是什么?你是Console.log 它!因此,我将告诉您 Console.log 的一些替代方案。
使用解构
通过使用 javascript 对象的破坏能力,您可以这样做:
const { log } = console;log("hi");log("testing");你可以将log函数更改为您想要的任何其他名称,如下所示:
const { log: myLog } = console;myLog("hi");myLog("testing");console.group
曾经想对您的日志进行分组吗?这个方法很适合你!
console.group("groupName");console.log("hi");console.log("testing");console.groupEnd();console.group("groupName2");console.log("hi");console.log("testing");console.groupEnd();
不错吧?
console.table
用于打印数组。
const arr = [1, 2, 3, 4, 5];console.table(arr);
console.time
此方法用于测量时间。例如,检查完成 x 任务花了多少秒?
console.time("test");setTimeout(() => { console.timeEnd("test");}, 1000);这将为我们提供以下结果:
test: 1.000sconsole.assert
此方法用于检查条件是否为真。如果不是,它将引发错误。
console.assert(/** Condition **/, /** Error message **/);console.assert(1 === 1, "1 is equal to 1"); // No errorconsole.assert(0 === [], "0 is equal to []"); // Error in the console
console.count
计算某件事发生的次数。
console.count("counter 1");for (let i = 0; i < 10; i++) { i % 2 == 0 ? console.count("counter 1") : console.count("counter 2");}这给了我们以下结果:

console.trace
此方法跟踪它被调用的位置。我有一个这样的 HTML 文件:
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> </head> <body> <script> "test"; console.trace(); i = 0; </script> </body></html>现在你可以看到在哪里 console.trace 被调用了。

debugger 关键词
您可能没有听说过该debugger关键字。它是一个用于停止代码执行的关键字。
const buggyCode = () => { debugger; console.log("hi");};// ...buggyCode();console.log("World");这会暂停代码的执行,您可以看到如下内容:

它会告诉您调试器的调用位置。

谢谢阅读!
感谢你阅读本教程。希望您已经学到了一些新东西并准备好开始使用它。
边栏推荐
- DNS分离解析和智能解析
- DOM-DOM树,一个DOM树有三种类型的节点
- 音频编解码,利用FAAC来实现AAC编码
- 输入起始位置,终止位置截取链表
- MSP430如何给板子下载程序?(IAR MSPFET CCS)
- Logstash日志数据写入异常排查问题总结
- Goodbye Chengdu paper invoices!The issuance of electronic invoices for accommodation expenses will soon completely replace the invoices of hotels, catering and gas stations
- BUU刷题记录
- DOM-DOM tree, a DOM tree has three types of nodes
- [DB operation management/development solution] Shanghai Daoning provides you with an integrated development tool to improve the convenience of work - Orange
猜你喜欢

"Life Is Like First Seen" is ill-fated, full of characters, and the contrast of Zhu Yawen's characters is too surprising

代码 Revert 后再次 Merge 会丢失的问题,已解决

rac备库双节点查询到的表最后更新时间不一致

Idea (preferred) cherry-pick operation

多商户商城系统功能拆解26讲-平台端分销设置

互换性与测量技术——表面粗糙度选取和标注方法

正式发布丨VS Code 1.70

Official release丨VS Code 1.70

ES6 advanced string processing new features

Vim and copy and paste from the outside (don't need to install the plugin)
随机推荐
音视频开发,为什么要学习FFmpeg?应该怎么入手FFmpeg学习?
关于地图GIS开发事项的一次实践整理(上)
《如何戒掉坏习惯》读书笔记
构建程序化交易系统需要注意什么问题?
求和、计数的窗口函数应用
The most unlucky and the luckiest
JS-DOM element object
按摩椅控制板的开发让按摩椅变得简约智能
IDE编译报错:Dangling metacharacter
Ninjutsu_v3_08_2020 - safety penetrating system installation
IDE compilation error: Dangling metacharacter
Goodbye Chengdu paper invoices!The issuance of electronic invoices for accommodation expenses will soon completely replace the invoices of hotels, catering and gas stations
Typescript study notes | Byte Youth Training Notes
2022-08-10 第六小组 瞒春 学习笔记
Entity到Vo的转换
ifconfig与ip命令的比较
否定语义转化层
BUU刷题记录
df和df -lh的意思
程序化交易与主观交易对盈利曲线的影响!