如何超越console.log并充分利用浏览器的调试控制台
by Gilad Dayagi
通过吉拉德·达亚吉
The console
object is a very useful feature of browsers that has been around for many years. It provides access to the browser’s debugging console.Most web developers know how to print messages to the console using console.log
. But I’ve found that many don’t know about other features of the console
, even though they can be very useful for every web developer.
console
对象是已经存在多年的浏览器的一项非常有用的功能。 它提供对浏览器调试控制台的访问。大多数Web开发人员都知道如何使用console.log
将消息打印到控制台。 但是我发现许多人都不知道console
其他功能,即使它们对于每个Web开发人员都非常有用。
In this post, I’ll go over some of these lesser known features and capabilities. I hope that you will find them useful and interesting, and will incorporate them into your day to day workflow and code.
在本文中,我将介绍一些鲜为人知的功能。 我希望您会发现它们有用且有趣,并将它们纳入您的日常工作流程和代码中。
I added a screenshot of the result of each example. If you want to try things for yourself, just open the DevTools and copy-paste the examples.
我添加了每个示例结果的屏幕截图。 如果您想自己尝试,只需打开DevTools并复制粘贴示例即可。
使用多个参数 (Using multiple arguments)
It is quite common to log several values together. These may be a message along with a related value or the content of several related variables.
将多个值记录在一起是很常见的。 这些可能是一条消息以及相关值或几个相关变量的内容。
Here are two ways I’ve seen developers achieve this:
我见过开发人员实现此目标的两种方法:
1.字符串串联 (1. String concatenation)
const a = 123;const b = 'abc';const c = {aa: 234, bb: 345};console.log('Foo bar ' + a + ' ' + b + ' ' + c);
2.使用多个通话 (2. Using multiple calls)
const a = 123;const b = 'abc';const c = {aa: 234, bb: 345};console.log('Foo bar');console.log(a);console.log(b);console.log(c);
These methods may work (sort of), but:
这些方法可能有效(但),但:
- They are not flexible他们不灵活
- They are not very readable他们不是很可读
- They are cumbersome to write他们写起来很麻烦
- They need special means to work properly with object variables他们需要特殊的方法才能正确处理对象变量
There are several better alternatives for outputting many variables. The most useful one for quick data dump is sending multiple arguments to console.log
, like so:
对于输出许多变量,有几种更好的选择。 快速数据转储最有用的方法是将多个参数发送到console.log
,如下所示:
const a = 123;const b = 'abc';const c = {aa: 234, bb: 345};console.log('Foo bar', a, b, c);
This is very handy for debugging, but the output is not very controllable. For output that is intended to be read (like for a library), I would use a different method, which we’ll get to later on.
这对于调试非常方便,但是输出不是很可控。 对于要读取的输出(例如对于库),我将使用其他方法,稍后将进行介绍。
使用不同的日志级别 (Using different log levels)
Besides the familiar console.log
, there are other logging methods that correspond to different log levels:
除了熟悉的console.log
,还有其他日志记录方法对应于不同的日志级别:
console.debug('Debug message');console.info('Info message');console.log('Good old log message');console.warn('A warning message');console.error('This is an error');
Each log level may have a different default style, which makes spotting errors and warnings at a glance easier.
每个日志级别可能具有不同的默认样式,这使发现错误和警告一目了然。
You can usually also filter which log levels you want to be visible in the DevTools console. This may help reduce clutter.
通常,您还可以过滤要在DevTools控制台中显示的日志级别。 这可能有助于减少混乱。
The appearance of the different levels and the filtering granularity changes from browser to browser.
不同级别的外观和过滤粒度随浏览器的不同而变化。
分组控制台行 (Grouping console lines)
Some times it is useful to group log messages together. It may allow for more organised and readable output.
有时将日志消息分组在一起很有用。 它可能允许更有组织性和可读性的输出。
This is actually very simple to achieve:
这实际上很容易实现:
console.group();console.log('First message');console.log('Second message');console.groupEnd();
Note that log groups can also be nested and labeled:
请注意,日志组也可以嵌套和标记:
console.group('Group aaa');console.log('First message');console.group('Group bbb');console.log('level 2 message a');console.log('Level 2 message b');console.groupEnd();console.log('Second message');console.groupEnd();
In case you want the group to appear collapsed, use console.groupCollapsed()
如果您希望组显示为折叠状态,请使用console.groupCollapsed()
衡量绩效 (Measuring performance)
Measuring the time between points in the code can serve as a quick way to check the performance of some operations.
测量代码中两点之间的时间可以用作检查某些操作性能的快速方法。
Here is a trivial way to do this:
这是一种简单的方法:
const start = Date.now();// do some stuffconsole.log('Took ' + (Date.now() - start) + ' millis');
This works, but there’s a more elegant way to achieve something similar:
这行得通,但是有一种更优雅的方法可以实现类似的目的:
console.time('Label 1');// do some stuffconsole.timeEnd('Label 1');
The code is shorter, the measurement is more accurate, and you can keep track of up to 10,000 different timers in parallel on a page.
代码更短,测量更准确,并且您可以在一个页面上并行跟踪多达10,000个不同的计时器。
字符串替换 (String substitution)
Previously we learned that you can pass multiple arguments to console.log
to output multiple values simultaneously. Another way to achieve something similar is to use string substitution. This method requires familiarity with the available placeholders, but offers greater control over the output.
先前我们了解到可以将多个参数传递给console.log
来同时输出多个值。 实现类似目的的另一种方法是使用字符串替换。 此方法需要熟悉可用的占位符,但可以更好地控制输出。
const a = 123;const b = 'abc';const c = {aa: 234, bb: 345};console.log('number %d string %s object %o', a, b, c);
Take a look at the documentation (link in the end) for a list of available placeholders.
查看文档(末尾链接)以获取可用占位符列表。
造型 (Styling)
It can be nice to style different log messages differently to increase readability.
为不同的日志消息设置不同的样式以提高可读性可能会很好。
We already mentioned that browsers give different default styling to some log levels, but this can also be customized according to your specific needs. Styling is done using a subset of CSS rules, passed in a string as the second parameter, and applied using the marker %c
.
我们已经提到过,浏览器为某些日志级别提供了不同的默认样式,但是也可以根据您的特定需求进行自定义。 样式是使用CSS规则的子集完成的,将字符串作为第二个参数传递,并使用标记%c
。
Note that you can have different styles for different parts of the log message.
请注意,日志消息的不同部分可以具有不同的样式。
For example:
例如:
console.log("Normal %cStyled %clorem %cipsum", "color: blue; font-weight: bold", "color: red", "background-image: linear-gradient(red, blue); color: white; padding: 5px;");
摘要 (Summary)
In this post we have seen some of the features of console
that I think are less well-known and more useful. This is by no means an exhaustive list of everything the console
can do, as it has many more tricks up its sleeve.
在这篇文章中,我们看到了一些console
功能,我认为这些功能较不为人所知,但更有用。 这绝不是console
可以做的所有事情的详尽清单,因为它还有很多技巧。
If this got you interested and you want to find out what other things you can do with the console
, I recommend reading the relevant documentation on MDN and trying things out in DevTools.
如果这引起了您的兴趣,并且您想了解console
还可以做些什么,我建议您阅读有关MDN的相关文档,并尝试在DevTools中进行尝试。
If you found this useful please share this article on social media.You can also follow me on twitter (@giladaya). Thanks for reading!
如果您觉得这很有用,请在社交媒体上分享此文章。 您也可以在Twitter(@giladaya)上关注我。 谢谢阅读!
翻译自: https://www.freecodecamp.org/news/how-to-go-beyond-console-log-and-get-the-most-out-of-your-browsers-debugging-console-e185256a1115/
相关文章:

区域设置 ID (LCID) 表, 及获取方法
区域设置 ID (LCID) 表, 及获取方法 中国的区域设置 ID 是 2052, 如果经常打开微软软件的安装目录应该经常见到.获取很简单, 有现成的 API 函数: GetThreadLocale.beginShowMessage(IntToStr(GetThreadLocale)); //2052 end; 区域设置 ID (LCID) 表区域设置描述简写十六进制值十…

E201700525-hm
skeleton n. 骨骼; (建筑物等的) 骨架; 梗概; 骨瘦如柴的人(或动物);adj. 骨骼的; 骨瘦如柴的; 概略的; 基本的; cloud n. 云; 云状物; invoke vt. 乞灵,祈求; 提出或授引…以支持或证明; 召鬼; 借助;render …

php不显示报错
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 error_reporting(E_ALL & ~E_NOTICE);

致谢 开源开发者的贡献_对开源做出的贡献如何使我成为更好的开发人员,以及如何做到这一点...
致谢 开源开发者的贡献by Luciano Strika通过卢西亚诺斯特里卡(Luciano Strika) 对开源做出的贡献如何使我成为更好的开发人员,以及如何做到这一点 (How contributing to open source made me a better developer — and how you can do it, too) So you’ve been …

欲精一行,必先通十行
将前端开发和服务器端开发做一个比较,前端开发没有服务器端开发“深”,服务器端开发没有前端开发“广”。经常听到做前端的同行抱怨需要学的东西太 多,东学一点西学一点,什么都会,但也什么都不精。很直接的结果就是沦为…

LeetCode 228: Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges. For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"]. 代码要求对数组中的元素进行分段。 首先给出字符串格式化函数,假设be…

JQ+ajax 提交表单不跳转页面
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 代码 <div class"apply_box"><h1>合作申请</h1><div class"apply_l"><input type"text" maxlength"20" id"name" name&q…

node.js是开源的吗_为开源做贡献并不难:我为Node.js项目做贡献的旅程
node.js是开源的吗As a developer, you should consider contributing to open source software. Many of your potential employers will look favorably on these contributions.作为开发人员,您应该考虑为开源软件做贡献。 您的许多潜在雇主将对这些供款看好。 …

超级简单的jquery轮播图demo
<!DOCTYPE html> <html><head><meta charset"UTF-8"><title>轮播图demo</title><script type"text/javascript" src"js/jquery-3.2.1.slim.js" ></script><link rel"stylesheet" …

Mysql 操作技巧
复制表结构 表数据Mysql> create tables t2 like t1;Mysql> insert into t2 select * from t1; mysql 索引a、Alert Table 用来创建普通索引、Unique 唯一索引 (当前列数值不可重复) 或 Primary Key 主键索引Mysql> alter table table_name add index index_name(col…

JS实现复制到剪切板效果
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 代码: <!DOCTYPE html> <html lang"en"><head><meta http-equiv"Content-Type" content"text/html; charsetutf-8" /><meta cha…

如何管理多个Python版本和虚拟环境
Addition January 2019: If you are coming back to this blog after upgrading to macOS Mojave please see this github issue for a solution to the common pyenv ‘zlib not available’ problem.此外,2019年1月:如果在升级到macOS Mojave之后又回到…

linux 下byte,char,unsigned char的区别
在linux中,对byte的定义为无符号char,而char默认为有符号char。 #ifndef BYTE #define BYTE unsigned char #endif以下ZZ百度知道: 在C中,默认的基础数据类型均为signed,现在我们以char为例,说明(signed) c…

词法作用域和动态作用域
JavaScript采用的是词法作用域 1.词法作用域 即函数定义时,即确定的作用域。js中的作用域链,在函数声明时候,就已经确定了,无论函数在何处调用,其作用域变量的查找都是按照定义是包含关系去查找。 2.动态作用域 变量的…

10-18 JS基础复习笔记
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 1.JS类型 Numbel String Boolean Symbol Null Undefined Object(Funtion,Array,Data,Regexp); 2.字符串转数字类型 "122" //122 var a 1 2; console.log(a) //3 3.null 和 u…

vue.js crud_如何使用VS Code和ADO.NET使用ASP.NET Core执行CRUD操作
vue.js crud介绍 (Introduction) In this article we are going to create a web application using ASP.NET Core MVC with the help of Visual Studio Code and ADO.NET. We will be creating a sample Employee Record Management System and performing CRUD operations on…
redis事物命令示例
命令示例: 1. 事务被正常执行:#在Shell命令行下执行Redis的客户端工具。/> redis-cli#在当前连接上启动一个新的事务。redis 127.0.0.1:6379>multiOK#执行事务中的第一条命令,从该命令的返回结果可以看出,该命令并没有立即执…

JS 函数 函数递归
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 重要:函数也是对象,你可以给它们添加属性或者更改它们的属性。 函数内部对象:arguments 解析:函数实际上是访问了函数体中一个名为 arguments 的内部对象…

swift设置启动图不现实_如何通过装饰房屋来开始在Swift中使用增强现实
swift设置启动图不现实by Ranadhir Dey由Ranadhir Dey 如何通过装饰房屋来开始在Swift中使用增强现实 (How to get started with augmented reality in Swift by decorating your home) If you’ve read my previous post, you already have a beautiful AR floor in your din…

可用于nodejs的SuperAgent(ajax API)
简单示例: import request from superagent;//引用声明 request.post(api).withCredentials()//跨域.end((err, res) > {if (res.ok) {const json JSON.parse(res.text);} else {console.log(获取失败);}}); 1、get 方式 当使用get请求传递查询字符串的时候&…

Java第四次实验
一、实验内容: 结对编程。运行TCP代码,结对进行,一人服务器,一人客户端;利用加解密代码包,编译运行代码,一人加密,一人解密; 集成代码,密后通过TCP发送。 二、…

JS 面向对象编程之原型(prototype)
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 function Person(first, last) {this.first first;this.last last; } Person.prototype.fullName function() {return this.first this.last; } Person.prototype.fullNameReversed function() {…

idea自动捕获_Smilefie:如何通过检测微笑来自动捕获自拍
idea自动捕获by Rishav Agarwal通过里沙夫阿加瓦尔 Smilefie:如何通过检测微笑来自动捕获自拍 (Smilefie: how you can auto-capture selfies by detecting a smile) Ten second takeaway: use Python and OpenCV to create an app that automatically captures a …

hiho 1015 KMP算法 CF 625 B. War of the Corporations
#1015 : KMP算法 时间限制:1000ms单点时限:1000ms内存限制:256MB描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进。 这一天,他们遇到了一只河蟹&#…

React 组件绑定点击事件,并且传参完整Demo
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 1.传数组下标给点击事件Demo: const A 65 // ASCII character codeclass Alphabet extends React.Component {constructor(props) {super(props);this.handleClick this.handleClick.bind…

ScaleYViewPager
https://github.com/eltld/ScaleYViewPager 转载于:https://www.cnblogs.com/eustoma/p/4572925.html

node mongoose_如何使用Express,Mongoose和Socket.io在Node.js中构建实时聊天应用程序
node mongooseby Arun Mathew Kurian通过阿伦马修库里安(Arun Mathew Kurian) 如何使用Express,Mongoose和Socket.io在Node.js中构建实时聊天应用程序 (How to build a real time chat application in Node.js using Express, Mongoose and Socket.io) In this tut…

结对项目开发电梯调度 - 整体设计
一、系统介绍 1. 功能描述 本电梯系统用来控制一台运行于一个具有16层的大楼电梯,它具有上升、下降、开门、关门、载客的基本功能。 大楼的每一层都有: (1) 两个指示灯: 这两个指示灯分别用于指示当前所在的层数和电梯的当前状态ÿ…

3.分支结构与循环结构
1 程序结构 程序结构分为顺序结构、分支结构、循环结构。分支结构有:if结构,if....else结构,if...else if....else ,if...else结构,switch结构;循环结构有:while循环,do....while循环…

微信小程序 实现复制到剪贴版功能
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 实现API: wx.setClipboardData(Object object) API说明:设置系统剪贴板的内容 属性类型默认值是否必填说明支持版本datastring 是剪贴板的内容 successfunction 否接口调用成功…