javascript控制台_如何使用JavaScript控制台改善工作流程

javascript控制台

by Riccardo Canella

里卡多·卡内拉(Riccardo Canella)

如何使用JavaScript控制台改善工作流程 (How you can improve your workflow using the JavaScript console)

As a web developer, you know very well the need to debug your code. We often use external libraries for logs, and to format and/or display them in some cases, but the console of our browsers is much more powerful than we think.

作为Web开发人员,您非常了解调试代码的需求。 我们经常使用外部库来记录日志,并在某些情况下格式化和/或显示它们,但是浏览器的控制台功能比我们想象的要强大得多。

When we think about the console, the first thing that comes to mind and the console.log, right? But there are many more methods than those we imagine. Now we will see how to make the most of using the console, and I’ll give you some tips to make them these methods more readable

当我们想到控制台时,首先想到的是console.log ,对吧? 但是有比我们想象的更多的方法。 现在,我们将了解如何充分利用控制台,并且我将为您提供一些技巧,以使这些方法更具可读性

什么是控制台? (What is the Console?)

The JavaScript console is a built-in feature in modern browsers that comes with out-of-the-box development tools in a shell-like interface. It allows a developer to:

JavaScript控制台是现代浏览器中的内置功能,它在类似Shell的界面中带有开箱即用的开发工具。 它允许开发人员执行以下操作:

  • View a log of errors and warnings that occur on a web page.

    查看网页上出现的错误和警告日志。
  • Interact with the web page using JavaScript commands.

    使用JavaScript命令与网页进行交互。
  • Debug applications and traverse the DOM directly in the browser.

    调试应用程序并直接在浏览器中遍历DOM。
  • Inspect and analyze network activity

    检查并分析网络活动

Basically, it empowers you to write, manage, and monitor JavaScript right within your browser.

基本上,它使您可以直接在浏览器中编写,管理和监视JavaScript。

Console.log,Console.error,Console.warn和Console.info (Console.log, Console.error, Console.warn and Console.info)

These are probably the most used methods of all. You can pass more than one parameter to these methods. Each parameter is evaluated and concatenated in a string delimited by the space, but in case of objects or arrays you can navigate between their properties.

这些可能是最常用的方法。 您可以将多个参数传递给这些方法。 每个参数都以空格分隔的字符串进行评估和连接,但是对于对象或数组,您可以在其属性之间导航。

Console.group (Console.group)

This method allows you to group a series of console.logs (but also error info, and so on) under a group that can be collapsed. The syntax is really very simple: just enter all the console.log we want to group before a console.group() (or console.groupCollapsed() if we want it to be closed by default). Then add a console.groupEnd() at the end to close the group.

通过此方法,您可以将一系列console.logs(还包括错误信息,等等)归为一个可折叠的组。 语法真的很简单:只需输入所有console.log一个之前我们要组console.group()console.groupCollapsed()如果我们希望它在默认情况下关闭)。 然后在末尾添加console.groupEnd()以关闭组。

The results will look like this:

结果将如下所示:

控制台表 (Console.table)

Since I discovered the console.table my life has changed. Displaying JSON or very large JSON arrays inside a console.log is a terrifying experience. The console.table allows us to visualize these structures inside a beautiful table where we can name the columns and pass them as parameters.

自从发现console.table我的生活发生了变化。 在console.log显示JSON或非常大的JSON数组是一种令人恐惧的体验。 console.table允许我们在漂亮的表中可视化这些结构,我们可以在其中命名列并将它们作为参数传递。

The result is wonderful and very useful in debugging:

结果是极好的,并且在调试中非常有用:

Console.count,Console.time和Console.timeEnd (Console.count, Console.time and Console.timeEnd)

These three methods are the Swiss army knife for every developer who needs to debug. The console.count counts and outputs the number of times that count() has been invoked on the same line and with the same label. The console.time starts a timer with a name specified as an input parameter, and can run up to 10,000 simultaneous timers on a given page. Once initiated, we use a call to console.timeEnd to stop the timer and print the elapsed time to the Console.

对于需要调试的每个开发人员,这三种方法都是瑞士军刀。 console.count计数并输出在同一行上使用相同标签调用count()的次数。 console.time以指定为输入参数的名称启动计时器,并且在给定页面上最多可以同时运行10,000个计时器。 启动后,我们将使用对console.timeEnd的调用来停止计时器并将经过的时间打印到控制台。

The output will look like this:

输出将如下所示:

Console.trace和Console.assert (Console.trace and Console.assert)

These methods simply print a stack trace from the point where it was called. Imagine you are creating a JS library and want to inform the user where the error was generated. In that case, these methods can be very useful. The console.assert is like the console.trace but will print only if the condition passed didn’t pass.

这些方法只是从调用点打印堆栈跟踪。 假设您正在创建一个JS库,并且想通知用户错误发生的位置。 在这种情况下,这些方法可能非常有用。 console.assert类似于console.trace但仅在传递的条件未通过时才会打印。

As we can see, the output is exactly what React (or any other library) would show us when we generate an exception.

如我们所见,输出正是生成异常时React(或任何其他库)向我们显示的内容。

删除所有控制台? (Delete all the Consoles ?)

Using consoles often forces us to eliminate them. Or sometimes we forget about the production build (and only notice them by mistake days and days later). Of course, I do not advise anyone to abuse consoles where they are not needed (the console in the change input handle can be deleted after you see that it works). You should leave error logs or trace logs in development mode to help you debug. I use Webpack a lot, both at work and in my own projects. This tool allows you to delete all the consoles that you do not want to remain (by type) from the production build using the uglifyjs-webpack-plugin ?

使用控制台通常会迫使我们消除它们。 有时我们会忘记生产版本(并且几天后才错误地注意到它们)。 当然,我不建议任何人滥用不需要控制台的控制台(在看到更改输入句柄后可以删除控制台)。 您应该将错误日志或跟踪日志留在开发模式下,以帮助您调试。 无论是在工作中还是在我自己的项目中,我都经常使用Webpack。 使用此工具,您可以使用uglifyjs-webpack-plugin从生产版本中删除所有不想保留的控制台(按类型)。

const UglifyJsPlugin = require('uglifyjs-webpack-plugin')var debug = process.env.NODE_ENV !== "production";.....optimization: {        minimizer: !debug ? [            new UglifyJsPlugin({                // Compression specific options                uglifyOptions: {                    // Eliminate comments                    comments: false,                    compress: {                       // remove warnings                       warnings: false,                       // Drop console statements                       drop_console: true                    },                }           })] : []}

The configuration is really trivial and it simplifies the work, so have fun with the console (but do not abuse it!)

配置确实很简单,并且简化了工作,因此可以在控制台上玩乐(但不要滥用它!)

If you liked the article please clap and follow me :)

如果您喜欢这篇文章,请鼓掌并关注我:)

Thx and stay tuned ?

谢谢,敬请期待?

Follow my last news and tips on Facebook

在Facebook上关注我的最新消息和提示

翻译自: https://www.freecodecamp.org/news/how-you-can-improve-your-workflow-using-the-javascript-console-bdd7823a9472/

javascript控制台

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/394116.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

appium===setup/setupclass的区别,以及@classmathod的使用方法

一、装饰器 1.用setUp与setUpClass区别 setup():每个测试case运行前运行 teardown():每个测试case运行完后执行 setUpClass():必须使用classmethod 装饰器,所有case运行前只运行一次 tearDownClass():必须使用classmethod装饰器,所有case运行完后只运行一次 2.是修饰符&#xf…

cache failed module status_Flutter混编之路——iOS踩坑记录

一、运行Xcode编译或者flutter run/build 过程中报错:"x86_64" is not an allowed value for option "ios-arch".解决方案在Debug.xcconfig中指定 “FLUTTER_BUILD_MODEdebug”,Release.xcconfig中指定“FLUTTER_BUILD_MODErelease”…

【最短路径Floyd算法详解推导过程】看完这篇,你还能不懂Floyd算法?还不会?...

简介 Floyd-Warshall算法(Floyd-Warshall algorithm),是一种利用动态规划的思想寻找给定的加权图中多源点之间最短路径的算法,与Dijkstra算法类似。该算法名称以创始人之一、1978年图灵奖获得者、斯坦福大学计算机科学系教授罗伯特…

java object类的常用子类_Java中Object类常用的12个方法,你用过几个?

前言Java 中的 Object 方法在面试中是一个非常高频的点,毕竟 Object 是所有类的“老祖宗”。Java 中所有的类都有一个共同的祖先 Object 类,子类都会继承所有 Object 类中的 public 方法。先看下 Object 的类结构(快捷键:alt7):1.…

leetcode面试题 04.12. 求和路径(dfs)

给定一棵二叉树,其中每个节点都含有一个整数数值(该值或正或负)。设计一个算法,打印节点数值总和等于某个给定值的所有路径的数量。注意,路径不一定非得从二叉树的根节点或叶节点开始或结束,但是其方向必须向下(只能从父节点指向子…

javaweb学习总结(二十二)——基于Servlet+JSP+JavaBean开发模式的用户登录注册

一、ServletJSPJavaBean开发模式(MVC)介绍 ServletJSPJavaBean模式(MVC)适合开发复杂的web应用,在这种模式下,servlet负责处理用户请求,jsp负责数据显示,javabean负责封装数据。 ServletJSPJavaBean模式程序各个模块之间层次清晰&…

2018黄河奖设计大赛获奖_宣布我们的freeCodeCamp 2018杰出贡献者奖获奖者

2018黄河奖设计大赛获奖by Quincy Larson昆西拉尔森(Quincy Larson) 宣布我们的freeCodeCamp 2018杰出贡献者奖获奖者 (Announcing Our freeCodeCamp 2018 Top Contributor Award Winners) Over the past 3 years, freeCodeCamp.org has grown from a small open source proje…

Log4j配置详解

来自: http://www.blogjava.net/zJun/archive/2006/06/28/55511.html Log4J的配置文件(Configuration File)就是用来设置记录器的级别、存放器和布局的,它可接keyvalue格式的设置或xml格式的设置信息。通过配置,可以创建出Log4J的运行环境。1. 配置文件 …

cors数据类型_如何根据RTK的差分格式选择千寻cors账号的源节点进行设置?

千寻cors账号的设置中源节点是根据使用的品牌RTK是为双星仪器还是三星仪器选择,但问题就在于我们看到的RTK的技术参数中一般很少见到标注仪器的卫星系统,更多的是差分格式。其实千寻cors账号的源节点也可以根据RTK的差分格式进行选择,不过这两…

java swing 串口_ComTest 接收串口数据,并显示在文本框内,通过JavaSwing实现 Develop 265万源代码下载- www.pudn.com...

文件名称: ComTest下载 收藏√ [5 4 3 2 1 ]开发工具: Java文件大小: 3157 KB上传时间: 2016-09-21下载次数: 0提 供 者: 韩坤详细说明:接收串口数据,并显示在文本框内,通过JavaSwing实现-Receive serial data, and displayed in the t…

leetcode329. 矩阵中的最长递增路径(dfs)

给定一个整数矩阵,找出最长递增路径的长度。对于每个单元格,你可以往上,下,左,右四个方向移动。 你不能在对角线方向上移动或移动到边界外(即不允许环绕)。示例 1:输入: nums [[9,9,4],[6,6,8…

SQL大圣之路笔记——PowerDesigner之新建table、view、proc

1. 新建table、view、proc 转载于:https://www.cnblogs.com/allenzhang/p/6305564.html

用python绘制一条直线_python绘制直线的方法

本文实例为大家分享了python绘制直线的具体代码&#xff0c;供大家参考&#xff0c;具体内容如下#!/usr/bin/env pythonimport vtk# 绘制通用方法def myshow(linepolydata):# Now well look at it.lineMapper vtk.vtkPolyDataMapper()if vtk.VTK_MAJOR_VERSION < 5:lineMap…

测试驱动开发 测试前移_我如何以及为什么认为测试驱动开发值得我花时间

测试驱动开发 测试前移by Ronauli Silva通过罗纳利席尔瓦(Ronauli Silva) I first read about test driven development (TDD) in some technical reviews blog, but I barely read it (or thought about it). Why would people write tests first when they already knew the…

P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm

对于一个牛&#xff0c;它存在两种状态&#xff1a;1.处于联通分量 2.不处于联通分量。对于处于联通分量的牛&#xff0c;求出联通分量的大小&#xff1b;对于不处于联通分量的牛&#xff0c;求出其距离联通分量的路程联通分量大小。 不同的联通分量&#xff0c;染上不同的颜色…

ASP.NET MVC5+EF6+EasyUI 后台管理系统(1)-前言与目录(持续更新中...)

开发工具&#xff1a;VS2015(2012以上)SQL2008R2以上数据库 您可以有偿获取一份最新源码联系QQ:729994997 价格 666RMB 升级后界面效果如下&#xff1a; 日程管理 http://www.cnblogs.com/ymnets/p/7094914.html 任务调度系统界面 http://www.cnblogs.com/ymnets/p/5065154.h…

leetcode106. 从中序与后序遍历序列构造二叉树(dfs)

根据一棵树的中序遍历与后序遍历构造二叉树。注意: 你可以假设树中没有重复的元素。例如&#xff0c;给出中序遍历 inorder [9,3,15,20,7] 后序遍历 postorder [9,15,7,20,3] 返回如下的二叉树&#xff1a;3/ \9 20/ \15 7解题思路 根据后序遍历的最后一个元素是父节点&…

【FRDM-K64F学习笔记】使用ARM mbed和Keil MDK下载你的第一个程序

FRDM-K64F开发平台采用MK64FN1M0VLL12微控制器。该控制器包含一个带有浮点单元的ARM Cortex-M4内核。其最高工作频率为120MHz&#xff0c;具有256KB的RAM、1MB闪存以及许多其他外设。它非常适合大多数可以采用以太网、SD卡存储以及板载模拟-数字转换器的IoT应用。但是&#xff…

php 实时更新内容_亿级视频内容如何实时更新?优酷视频背后的技术揭秘

简介&#xff1a; 优酷视频内容数据天然呈现巨大的网络结构&#xff0c;各类数据实体连接形成了数十亿顶点和百亿条边的数据量&#xff0c;面对巨大的数据量&#xff0c;传统关系型数据库往往难以处理和管理&#xff0c;图数据结构更加贴合优酷的业务场景&#xff0c;图组织使用…

ios集成firebase_如何使用Firebase将Google Login集成到Ionic应用程序中

ios集成firebaseby Ryan Gordon通过瑞安戈登(Ryan Gordon) 如何使用Firebase将Google Login集成到Ionic应用程序中 (How to integrate Google Login into an Ionic app with Firebase) A lot of apps these days need to maintain some form of user authentication. This hel…