javascript调试_如何提高JavaScript调试技能
javascript调试
Almost all software developers who have written even a few lines of code for the Web have had at least a quick glance at JavaScript. After all, it is currently one of the most in-demand programming languages.
几乎所有甚至为Web编写了几行代码的软件开发人员,都至少可以快速浏览一下JavaScript。 毕竟,它是目前最受欢迎的编程语言之一。
Some people love it, some hate it. Regardless of your view, if you use it, you will need to debug it eventually.
有些人喜欢它,有些讨厌它。 无论您使用哪种视图,都必须最终对其进行调试。
Below I will share some tips that have helped me in difficult moments.
下面,我将分享一些在困难时刻对我有帮助的提示。
基本/知名的 (The basic / well-known ones)
橡皮鸭调试 (Rubber duck debugging)
Rubber duck debugging is a method where you explain your problem to anyone or anything (it doesn’t have to be a human). Then the solution magically stops playing with your goodwill and appears to you.
橡皮鸭调试是一种您可以向任何人或任何人解释问题的方法(不必是人类)。 然后,解决方案就神奇地停止了您的信誉,并向您显示。
The ‘duck’ has no knowledge of your project, so you explain everything, questioning your assumptions at the same time. Ideally, you’ll suddenly have an enlightenment like, ‘Oh dear, it was in front of me, thanks bro, sorry for the interruption.’
“鸭子”对您的项目一无所知,因此您可以解释所有内容,同时质疑您的假设。 理想情况下,您会突然得到启发,例如“哦,亲爱的,它就在我面前,谢谢兄弟,对不起您的打扰。”
Yet the duck was silent all this time, and that’s the magic part. :)
但是鸭子一直都保持沉默,这就是神奇的部分。 :)
好的日志记录 (The good ol’ logging)
When you have an issue to debug, you usually have a vague hypothesis of what might be wrong. It might be totally off from the actual cause, but this is another story. If you start putting logs in places where the error might occur, you may get to the point fast.
当您有问题要调试时,通常会有关于什么是错误的模糊的假设。 这可能与实际原因完全不符,但这是另一回事了。 如果您开始将日志放在可能发生错误的地方,则可能会很快达到目的。
Even if you don’t, don’t remove the logs you added, as they might prove themselves helpful on a future issue.
即使您不这样做,也不要删除添加的日志,因为它们可能会证明自己对解决以后的问题很有帮助。
I could argue why you should never reach this point, to add debug logs, as the logs should be there as part of the initial development. But Erik Hazard has already done the job.
我可能会争辩说,为什么您永远都不能添加调试日志,因为日志应该作为初始开发的一部分而存在。 但是Erik Hazard已经完成了这项工作。
More on logging later.
有关稍后记录的更多信息。
打破重点 (Breaking the points)
A debugger is a great tool in your arsenal and a great help — if you know how to use it.
如果您知道如何使用调试器,那么它是您的工具库中的一个很好的工具,也是一个很大的帮助。
That means:
这意味着:
- First understand the problem首先了解问题
Then make a couple of hypotheses about the root cause (and not the symptom)
然后对根本原因(而不是症状)做出一些假设
- Set the appropriate breakpoints to verify or disprove them.设置适当的断点以验证或取消验证它们。
In JavaScript, you can either set in the browser’s dev tool or use the debugger keyword in the code to force pausing the execution.
在JavaScript中,您可以在浏览器的dev工具中进行设置,也可以在代码中使用debugger关键字来强制暂停执行。
So don’t just put random breakpoints here and there. Have a routine and an ‘end’ in your mind when using it.
因此,不要随便在此处放置随机断点。 使用它时,请记住一个例程和一个“目标”。
不太知名的 (The less well-known ones)
控制台表 (console.table)
A few lines above, we spoke about the importance of logging. The command we usually use is console.log('text')
. But what if the output is more complex? Yes, console.log
handles arrays, too. And objects.
在上面的几行中,我们谈到了日志记录的重要性。 我们通常使用的命令是console.log('text')
。 但是,如果输出更复杂怎么办? 是的, console.log
处理数组。 和对象。
But what if I told you that you could spot the error faster because of some…beautification? That would be console.table
method and is demonstrated below
但是,如果我告诉您,由于某些……美化,您可以更快地发现错误,该怎么办? 那将是console.table
方法,并在下面进行演示
See what a nice overview you can gain from the layout? I highly encourage you to use it more, especially with iterables.
看到您可以从布局中获得什么样的概览? 我强烈建议您多使用它,尤其是在可迭代对象上。
打破事件而不是排队 (Break on events instead of lines)
Let’s imagine the following scenario. A DOM element is changing intermittently and with wrong values, and you have no clue why. Which of the 29 functions that mutate it is being naughty? (Side note: Mutating is bad usually, but this is a topic for another blog post.)
让我们想象一下以下情况。 DOM元素会间歇性地更改并具有错误的值,您不知道为什么。 使它变异的29个功能中,哪个是调皮的? (旁注:变异通常是不好的 ,但这是另一篇博客文章的主题。)
Use the DOM change breakpoints. Every time the element is mutated, a stack track will be shown. Just like if you have put the correct breakpoints. You can find more details here.
使用DOM更改断点 。 每次更改元素时,都会显示一个堆栈轨道。 就像您已设置正确的断点一样。 您可以在此处找到更多详细信息。
剖析 (Profiling)
If the bug you are working on is not performance-oriented, maybe this is overkill. But I still have to mention it (well, it may be a performance issue after all :) ). In Chrome and Firefox you can use the profiler’s built-in functionality to spot a bottleneck or just…see the functions that are executed. Boom :). Overengineering at its best. Please use this feature wisely. Killing a fly with a bazooka can have some weird side effects.
如果您正在处理的错误不是面向性能的,那么这可能是过大了。 但是我还是不得不提(毕竟,可能是性能问题:))。 在Chrome和Firefox中,您可以使用探查器的内置功能来发现瓶颈,或者只是…查看执行的功能。 繁荣:)。 最好地进行过度工程。 请明智地使用此功能。 用火箭筒杀死苍蝇会产生一些怪异的副作用。
结论 (Conclusion)
Thank you for reading this article. I hope you enjoyed it and learned something today. As always, I highly encourage to share your magic techniques in the comments.
感谢您阅读本文。 我希望您喜欢它并今天学到一些东西。 与往常一样,我强烈建议在评论中分享您的魔术技巧。
更多阅读 (More reading)
Apart from the links cited inside the main text of the article, here are some more things I think are worth reading about the topic of debugging:
除了本文正文中引用的链接之外,还有一些我认为值得阅读的有关调试主题的内容:
Node debugging tutorial
节点调试教程
John Sonmez’s debugging guide
John Sonmez的调试指南
Debug it
调试一下
Debugging: The 9 Indispensable Rules for Finding Even the Most Elusive Software and Hardware Problems
调试:查找最难以捉摸的软件和硬件问题的9条必不可少的规则
Chrome debug tools
Chrome调试工具
Firefox debug tools
Firefox调试工具
‘JSparty’ podcast and especially episode 30 from where I got inspired about this post and learned about `console.table`
“ JSparty”播客,尤其是第30集,从这本书中我得到启发,并了解了“ console.table”
Originally published on my blog.
最初发布在我的博客上 。
翻译自: https://www.freecodecamp.org/news/stepping-up-your-javascript-debugging-skills-cb37355ea9a9/
javascript调试
相关文章:

Java transient
原文出自:http://www.importnew.com/21517.html 1. transient的作用及使用方法 我们都知道一个对象只要实现了Serilizable接口,这个对象就可以被序列化,java的这种序列化模式为开发者提供了很多便利,我们可以不必关系具体序列化的…

KBMMW 的日志管理器
kbmmw 4.82 最大的新特性就是增加了 日志管理器。 新的日志管理器实现了不同类型的日志、断言、异常处理、计时等功能。 首先。引用kbmMWLog.pas 单元后,系统就默认生成一个IkbmMWLog 实例: Log:IkbmMWLog; log 默认使用对应操作系统的日志功能。 为了能…

React 循环渲染 5
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 使用循环渲染的demo: const todoItems todos.map((todo) ><li key{todo.id}>{todo.text}</li> ); const todoItems todos.map((todo, index) >// Only do this if item…

面试时与人事交谈时间_如何与您的技术负责人交谈并解决通讯故障
面试时与人事交谈时间by Greg Sabo由格雷格萨博(Greg Sabo) 如何与您的技术负责人交谈并解决通讯故障 (How to talk to your tech lead and fix your communication glitches) Here’s where you messed up.这是你搞砸的地方。 Your tech lead told you to build out a new A…

inotify简介
一、inotify简介 inotify是Linux内核2.6.13 (June 18, 2005)版本新增的一个子系统(API),它提供了一种监控文件系统(基于inode的)事件的机制,可以监控文件系统的变化如文件修改、新增、删除等,并…

链路层寻址与 ARP
一、 MAC 地址 不是主机或路由器具有链路层地址,而是它们的适配器(即网络接口)具有链路层地址。因此,具有多个网络接口的主机或路由器将具有与之相关联的多个链路层地址。 然而,链路层交换机并不具有与它们接口相关联的…

React 开始制作 6
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 从模拟开始 第1步:将UI分解为组件层次结构 不同的颜色为不同的组件, 第2步:在React中构建静态版本 第3步:确定UI状态的最小(但完整ÿ…

php 空间类元素引入_引入单元素模式
php 空间类元素引入by Diego Haz迭戈哈兹(Diego Haz) 引入单元素模式 (Introducing the Single Element Pattern) 使用React和其他基于组件的库创建可靠的构建基块的规则和最佳实践。 (Rules and best practices for creating reliable building blocks with React and other …
Tcl学习之--列表|字典
【列表|字典】Tcl使用列表来处理各种集合,比方一个目录中的全部文件,以及一个组件的全部选项。最简单的列表就是包括由随意个空格、制表符、换行符、分隔的随意多个元素的字符串。比方: JerryAlice Mandy David l lindex命令: --> 获取元素 至少须要…

JAVA代码实现下载单个文件,和下载打包文件
//下载单个文件调用方法 /** * response * imgPath 下载图片地址 * fileName 保存下载文件名称 * date 2015年4月14日 下午5:53:24 */ public static void download(HttpServletResponse response,String imgPath,String fileName){ OutputStrea…

php读取本地xlsx格式文件的数据并按json格式返回
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 目的:php读取并操作本地xlsx格式的文件; 完整示例代码: 代码讲解:前端发起post网络请求,php接收一个name(姓名)的参数…

面向对象编程概念_如何向6岁的孩子解释面向对象的编程概念
面向对象编程概念by Alexander Petkov通过亚历山大佩特科夫(Alexander Petkov) Have you noticed how the same cliche questions always get asked at job interviews — over and over again?您是否注意到在求职面试中总是一遍又一遍地问同样的陈词滥调问题? I…

jQuery 属性
jQuery 属性 方法描述context在版本 1.10 中被废弃。包含被传递到 jQuery 的原始上下文jquery包含 jQuery 的版本号jQuery.fx.interval改变以毫秒计的动画运行速率jQuery.fx.off对所有动画进行全局禁用或启用jQuery.support包含表示不同浏览器特性或漏洞的属性集(主…

mongodb的几种启动方法
1 mongodb的几种启动方法 启动Mongodb服务有两种方式,前台启动或者Daemon方式启动,前者启动会需要保持当前Session不能被关闭,后者可以作为系统的fork进程执行,下文中的path是mongodb部署的实际地址。1. 最简单的启动方式…

php 修改数据库表的字段的值
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 php 前端传递参数,遍历数据库表的字段后根据条件进行修改。 <?phpheader("Content-Type:text/html;charsetutf8"); header("Access-Control-Allow-Origin: *"); //解…

如何开始使用PostgreSQL
by Akul Tomar通过Akul Tomar 如何开始使用PostgreSQL (How to get started with PostgreSQL) PostgreSQL is an open source Relational Database Management System (RDBMS). In this article, I’ll provide an introduction to getting started with PostgreSQL. Here is …

Java中数组常见的几种排序方法!
数组的定义: int[] arr new int[5];int[] arr1 {1,2,3,4,5};long[] arr2 new long[6];String[] strs new String[5];Person[] ps new Person[5]; 数组的操作: int[] arr {45, 34, 53, 43};Arrays.sort(arr);System.out.println(Arrays.toString(ar…

oracle 如何预估将要创建的索引的大小
一.1 oracle 如何预估将要创建的索引的大小 oracle 提供了2种可以预估将要创建的索引大小的办法: ① 利用包 Dbms_space.create_index_cost 直接得到 ② 利用11g新特性 Note raised when explain plan for create index 下边分别举例说明。 一.2 环境说明 [ora…

删除对象的某个属性
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 实现代码: var data {a:1,b:2,c:3}for(var item in data){if (item b) {delete data[item];} }console.log(data:, data) 打印结果: data: {a: 1, c: 3}

java 学到什么实习_我如何获得外展实习机会以及到目前为止所学到的知识
java 学到什么实习by Nguedia Adele由Nguedia Adele 我如何获得外展实习机会以及到目前为止所学到的知识 (How I got my Outreachy internship and what I’ve learned so far) I recently got accepted for an Outreachy internship, working with LibreHealth.我最近接受了与…

STM32F103C8开发板原理图和管脚图
转载于:https://www.cnblogs.com/libra13179/p/6894335.html

js实用数组方法
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 判断是否为数组 1. Array.isArray([]) 2. var arr[1,2] arr instanceof Array -->true arr instanceof String -->false map ---- 返回数组得出的结果 const filtered [1, 2,…

BasicModal - 简单易用的现代 Web App 弹窗
BasicModal 是为现代 Web 应用程序打造的弹窗系统。它包括所有你需要显示的信息,问题或接收用户的输入。这里的弹窗还可以链接起来,所以你可以很容易地建立一个预定义顺序的安装帮助或显示对话框。无效输入可以使用包含突出显示和处理功能。 在线演示 …

javascript选择器_如何通过选择正确JavaScript选择器来避免沮丧
javascript选择器选择器如何影响代码的快速指南 (A quick guide on how selectors affect your code) While working on a project, I ran into an issue in my code. I was attempting to define multiple HTML elements into a collection and then change those elements ba…

Asp.net中GridView使用详解(引)【转】
Asp.net中GridView使用详解(引) GridView无代码分页排序 GridView选中,编辑,取消,删除 GridView正反双向排序 GridView和下拉菜单DropDownList结合 GridView和CheckBox结合 鼠标移到GridView某一行时改变该行的背景色方法一 鼠标移到GridView…

《任正非:我若贪生怕死,何来让你们英勇奋斗》
非常高兴尼泊尔代表处的进步,你们的一个历史项目概算亏损,从大前年亏损2.7亿美金,到前年亏损3000万美金,到去年盈利2140万美金。在喜马拉雅南麓一路爬坡,辛苦了。听说去年你们都涨了工资,我十分高兴。巴西代…

个人使用微信支付
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 首先在PAYJS申请到商户号和密钥, 然后实现源码如下: <!DOCTYPE html> <html lang"zh"><head><meta charset"UTF-8"><title>…

构建node.js基础镜像_我如何使用Node.js构建工作抓取网络应用
构建node.js基础镜像by Oyetoke Tobi Emmanuel由Oyetoke Tobi Emmanuel 我如何使用Node.js构建工作抓取网络应用 (How I built a job scraping web app using Node.js) Scraping jobs from the web has now become easier thanks to Indreed.现在,借助Indreed&…

Robotium测试报告的生成方法(上)
7.1 使用junit-report生成报告 这个是参考网上的:http://www.xuebuyuan.com/2148574.html,经我个人验证是可行的方法,网上写的挺详细的,不过有些不太清楚明白的地方,鉴于网上说的有点迷茫,所以下面我再细化…

Python之向日志输出中添加上下文信息
除了传递给日志记录函数的参数(如msg)外,有时候我们还想在日志输出中包含一些额外的上下文信息。比如,在一个网络应用中,可能希望在日志中记录客户端的特定信息,如:远程客户端的IP地址和用户名。…