14 个你可能不知道的 JavaScript 调试技巧
以更快的速度和更高的效率来调试 JavaScript
熟悉工具可以让工具在工作中发挥出更大的作用。尽管江湖传言 JavaScript 很难调试,但如果你掌握了几个技巧,就能用很少的时间来解决错误和 bug。
文中已经列出了 14 个你可能不知道的调试技巧,但是可能需要你牢记在心,以便在下次需要调试 JavaScript 代码时使用!
一起来看。
大多数技巧都适用于 Chrome 控制台和 Firefox, 尽管还有很多其他的调试工具,但大部分也适用。
1. debugger;
除了console.log
, debugger;
是我们最喜欢的应急调试工具。执行代码后,Chrome 会在执行时自动停止。你甚至可以把它封装成条件,只在需要时才运行。
<span class="kwd">if</span><span class="pun">(</span><span class="pln">thisThing</span><span class="pun">)</span><span class="pun">{</span>
<span class="kwd">debugger</span><span class="pun">;</span>
<span class="pun">}</span>
2. 用表格显示对象
有时, 有一组复杂的对象要查看。可以通过console.log
查看并滚动浏览,亦或者使用console.table
展开,更容易看到正在处理的内容!
<span class="kwd">var</span><span class="pln"> animals </span><span class="pun">=</span><span class="pun">[</span>
<span class="pun">{</span><span class="pln"> animal</span><span class="pun">:</span><span class="str">'Horse'</span><span class="pun">,</span><span class="pln"> name</span><span class="pun">:</span><span class="str">'Henry'</span><span class="pun">,</span><span class="pln"> age</span><span class="pun">:</span><span class="lit">43</span><span class="pun">},</span>
<span class="pun">{</span><span class="pln"> animal</span><span class="pun">:</span><span class="str">'Dog'</span><span class="pun">,</span><span class="pln"> name</span><span class="pun">:</span><span class="str">'Fred'</span><span class="pun">,</span><span class="pln"> age</span><span class="pun">:</span><span class="lit">13</span><span class="pun">},</span>
<span class="pun">{</span><span class="pln"> animal</span><span class="pun">:</span><span class="str">'Cat'</span><span class="pun">,</span><span class="pln"> name</span><span class="pun">:</span><span class="str">'Frodo'</span><span class="pun">,</span><span class="pln"> age</span><span class="pun">:</span><span class="lit">18</span><span class="pun">}</span>
<span class="pun">];</span>
<span class="pln">console</span><span class="pun">.</span><span class="pln">table</span><span class="pun">(</span><span class="pln">animals</span><span class="pun">);</span>
输出:
Screenshot showing the resulting table for JavaScript debugging tip 2
3. 使用不同屏幕尺寸
在桌面上安装不同移动设备模拟器非常棒,但现实确是不可行的。如何调整窗口大小呢?Chrome 提供了所需的一切。跳到控制台并点击“切换设备模式”按钮。观察窗口变化即可!
4. 如何快速找到 DOM 元素
在 Elements 面板中标记一个 DOM 元素,并在控制台中使用它。Chrome 控制台会保留选择历史的最后五个元素,最终选择的首个元素被标记为$0
,第二个选择的元素为$1
,依此类推。
如果您按照 “item-4”,“item-3”,“item-2”,“item-1”,“item-0” 的顺序选择以下标签,则可以在控制台中访问 DOM 节点:
5. 使用 console.time()
和 console.timeEnd()
测试循环
要得知某些代码的执行时间,特别是调试缓慢循环时,非常有用。 甚至可以通过给方法传入不同参数,来设置多个定时器。来看看它是怎么运行的:
<span class="pln">console</span><span class="pun">.</span><span class="kwd">time</span><span class="pun">(</span><span class="str">'Timer1'</span><span class="pun">);</span>
<span class="kwd">var</span><span class="pln"> items </span><span class="pun">=</span><span class="pun">[];</span>
<span class="kwd">for</span><span class="pun">(</span><span class="kwd">var</span><span class="pln"> i </span><span class="pun">=</span><span class="lit">0</span><span class="pun">;</span><span class="pln"> i </span><span class="pun"><</span><span class="lit">100000</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">++){</span>
<span class="pln">items</span><span class="pun">.</span><span class="pln">push</span><span class="pun">({</span><span class="pln">index</span><span class="pun">:</span><span class="pln"> i</span><span class="pun">});</span>
<span class="pun">}</span>
<span class="pln">console</span><span class="pun">.</span><span class="pln">timeEnd</span><span class="pun">(</span><span class="str">'Timer1'</span><span class="pun">);</span>
运行产生了一下结果:
6. 获取函数的堆栈跟踪信息
使用 JavaScript 框架,会引入大量代码。
创建视图并触发事件,最后你想了解函数调用的过程。
由于 JavaScript 不是一个很结构化的语言, 有时候很难知道什么时候发生了什么。使用 console.trace
(仅仅只是在控制台中跟踪)可以方便地调试 JavaScript.
想象一下,要查看第 24 行car
实例调用函数funcZ
的整个堆栈跟踪信息:
<span class="kwd">var</span><span class="pln"> car</span><span class="pun">;</span>
<span class="kwd">var</span><span class="pln"> func1 </span><span class="pun">=</span><span class="kwd">function</span><span class="pun">()</span><span class="pun">{</span>
<span class="pln">func2</span><span class="pun">();</span>
<span class="pun">}</span>
<span class="kwd">var</span><span class="pln"> func2 </span><span class="pun">=</span><span class="kwd">function</span><span class="pun">()</span><span class="pun">{</span>
<span class="pln">func4</span><span class="pun">();</span>
<span class="pun">}</span>
<span class="kwd">var</span><span class="pln"> func3 </span><span class="pun">=</span><span class="kwd">function</span><span class="pun">()</span><span class="pun">{</span>
<span class="pun">}</span>
<span class="kwd">var</span><span class="pln"> func4 </span><span class="pun">=</span><span class="kwd">function</span><span class="pun">()</span><span class="pun">{</span>
<span class="pln">car </span><span class="pun">=</span><span class="kwd">new</span><span class="typ">Car</span><span class="pun">();</span>
<span class="pln">car</span><span class="pun">.</span><span class="pln">funcX</span><span class="pun">();</span>
<span class="pun">}</span>
<span class="kwd">var</span><span class="typ">Car</span><span class="pun">=</span><span class="kwd">function</span><span class="pun">()</span><span class="pun">{</span>
<span class="kwd">this</span><span class="pun">.</span><span class="pln">brand </span><span class="pun">=</span><span class="pun">‘</span><span class="pln">volvo</span><span class="pun">’;</span>
<span class="kwd">this</span><span class="pun">.</span><span class="pln">color </span><span class="pun">=</span><span class="pun">‘</span><span class="pln">red</span><span class="pun">’;</span>
<span class="kwd">this</span><span class="pun">.</span><span class="pln">funcX </span><span class="pun">=</span><span class="kwd">function</span><span class="pun">()</span><span class="pun">{</span>
<span class="kwd">this</span><span class="pun">.</span><span class="pln">funcY</span><span class="pun">();</span>
<span class="pun">}</span>
<span class="kwd">this</span><span class="pun">.</span><span class="pln">funcY </span><span class="pun">=</span><span class="kwd">function</span><span class="pun">()</span><span class="pun">{</span>
<span class="kwd">this</span><span class="pun">.</span><span class="pln">funcZ</span><span class="pun">();</span>
<span class="pun">}</span>
<span class="kwd">this</span><span class="pun">.</span><span class="pln">funcZ </span><span class="pun">=</span><span class="kwd">function</span><span class="pun">()</span><span class="pun">{</span>
<span class="pln">console</span><span class="pun">.</span><span class="pln">trace</span><span class="pun">(‘</span><span class="pln">trace car</span><span class="pun">’)</span>
<span class="pun">}</span>
<span class="pun">}</span>
<span class="pln">func1</span><span class="pun">();</span>
24 行将输出:
可以看到 func1
调用 func2
, func2
调用 func4
。 func4
创建了一个 car
的实例,然后调用函数 car.funcX
,依此类推。
即使你认为自己的代码写的非常好,这���然很有用。假如你想改进自己的代码。获取跟踪信息和所有涉及的函数,每一项都可以点击,可以在他们之间来回切换。就像是给你提供了一个调用堆栈的选择列表。
7. 将代码格式化后再调试 JavaScript
有时代码会在生产环境出问题,但是你的 source maps 没有部署在生产环境上。不要怕。Chrome 可以将您的 JavaScript 文件格式化。格式化后的代码虽然不像真实代码那样有用,但至少可以看到发生了什么。点击 Chrome 控制台中的源代码查看器中的{}
按钮即可。
8. 快速查找要调试的函数
假设你要在函数中打断点,最常用的两种方式是:
- 在控制台查找行并添加断点
- 在代码中添加
debugger;
在这两个解决方案中,您必须在文件中单击以调试特定行。
使用控制台打断点可能不太常见。在控制台中使用debug(funcName)
,当到达传入的函数时,代码将停止。
这个调试方法很快, 但缺点是不适用于私有或匿名函数。但除了私有和匿名函数, 这可能是找到调试函数的最快方法。(注意:这个函数和console.debug
函数不是同一个东西。)
<span class="kwd">var</span><span class="pln"> func1 </span><span class="pun">=</span><span class="kwd">function</span><span class="pun">()</span><span class="pun">{</span>
<span class="pln">func2</span><span class="pun">();</span>
<span class="pun">};</span>
<span class="kwd">var</span><span class="typ">Car</span><span class="pun">=</span><span class="kwd">function</span><span class="pun">()</span><span class="pun">{</span>
<span class="kwd">this</span><span class="pun">.</span><span class="pln">funcX </span><span class="pun">=</span><span class="kwd">function</span><span class="pun">()</span><span class="pun">{</span>
<span class="kwd">this</span><span class="pun">.</span><span class="pln">funcY</span><span class="pun">();</span>
<span class="pun">}</span>
<span class="kwd">this</span><span class="pun">.</span><span class="pln">funcY </span><span class="pun">=</span><span class="kwd">function</span><span class="pun">()</span><span class="pun">{</span>
<span class="kwd">this</span><span class="pun">.</span><span class="pln">funcZ</span><span class="pun">();</span>
<span class="pun">}</span>
<span class="pun">}</span>
<span class="kwd">var</span><span class="pln"> car </span><span class="pun">=</span><span class="kwd">new</span><span class="typ">Car</span><span class="pun">();</span>
在控制台中输入debug(car.funcY)
,当调用car.funcY
时,将以调试模式停止:
9. 屏蔽不相关代码
现在,我们经常在应用中引入几个库或框架。其中大多数都经过良好的测试且相对没有缺陷。 但是,调试器仍然会进入与调试任务无关的文件。解决方案是屏蔽不需要调试的脚本。当然可以包括你自己的脚本。在这篇文章中阅读更多关于调试不相关代码的内容 。
10. 在复杂的调试过程中寻找重点
在更复杂的调试中,我们有时希望输出很多行。可以做的就是保持良好输出结构,使用更多控制台函数,例如, console.log
, console.debug
, console.warn
, console.info
, console.error
等等。然后,可以在控制台中快速浏览。但有时候,某些 JavaScript 调试信息并不是你需要的。现在,可以自己美化调试信息了。在调试 JavaScript 时,可以使用 CSS 并自定义控制台信息:
<span class="pln">console</span><span class="pun">.</span><span class="pln">todo </span><span class="pun">=</span><span class="kwd">function</span><span class="pun">(</span><span class="pln">msg</span><span class="pun">)</span><span class="pun">{</span>
<span class="pln">console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(‘</span><span class="pun">%</span><span class="pln"> c </span><span class="pun">%</span><span class="pln"> s </span><span class="pun">%</span><span class="pln"> s </span><span class="pun">%</span><span class="pln"> s</span><span class="pun">‘,</span><span class="pun">‘</span><span class="pln">color</span><span class="pun">:</span><span class="pln"> yellow</span><span class="pun">;</span><span class="pln"> background </span><span class="pun">-</span><span class="pln"> color</span><span class="pun">:</span><span class="pln"> black</span><span class="pun">;’,</span><span class="pun">‘–‘,</span><span class="pln"> msg</span><span class="pun">,</span><span class="pun">‘–‘);</span>
<span class="pun">}</span>
<span class="pln">console</span><span class="pun">.</span><span class="pln">important </span><span class="pun">=</span><span class="kwd">function</span><span class="pun">(</span><span class="pln">msg</span><span class="pun">)</span><span class="pun">{</span>
<span class="pln">console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(‘</span><span class="pun">%</span><span class="pln"> c </span><span class="pun">%</span><span class="pln"> s </span><span class="pun">%</span><span class="pln"> s </span><span class="pun">%</span><span class="pln"> s</span><span class="pun">’,</span><span class="pun">‘</span><span class="pln">color</span><span class="pun">:</span><span class="pln"> brown</span><span class="pun">;</span><span class="pln"> font </span><span class="pun">-</span><span class="pln"> weight</span><span class="pun">:</span><span class="pln"> bold</span><span class="pun">;</span><span class="pln"> text </span><span class="pun">-</span><span class="pln"> decoration</span><span class="pun">:</span><span class="pln"> underline</span><span class="pun">;’,</span><span class="pun">‘–‘,</span><span class="pln"> msg</span><span class="pun">,</span><span class="pun">‘–‘);</span>
<span class="pun">}</span>
<span class="pln">console</span><span class="pun">.</span><span class="pln">todo</span><span class="pun">(“</span><span class="typ">This</span><span class="kwd">is</span><span class="pln"> something that</span><span class="pun">’</span><span class="pln"> s need to be fixed</span><span class="pun">”);</span>
<span class="pln">console</span><span class="pun">.</span><span class="pln">important</span><span class="pun">(‘</span><span class="typ">This</span><span class="kwd">is</span><span class="pln"> an important message</span><span class="pun">’);</span>
输出:
例如:
在console.log()
中, 可以用%s
设置字符串,%i
设置数字,%c
设置自定义样式等等,还有很多更好的console.log()
使用方法。 如果使用的是单页应用框架,可以为视图(view)消息创建一个样式,为模型(model)、集合(collection)、控制器(controller)等创建另一个样式。也许还可以像 wlog、clog 和 mlog 一样发挥想象力!
11. 观察特定函数的调用及参数
在 Chrome 控制台中,可以观察特定的函数。每次调用该函数,就会打印出传入的参数。
<span class="kwd">var</span><span class="pln"> func1 </span><span class="pun">=</span><span class="kwd">function</span><span class="pun">(</span><span class="pln">x</span><span class="pun">,</span><span class="pln"> y</span><span class="pun">,</span><span class="pln"> z</span><span class="pun">)</span><span class="pun">{</span>
<span class="com">//....</span>
<span class="pun">};</span>
输出:
这是查看传入函数参数的好方法。但是,如果控制台提示我们形参的数目就更好了。在上面的例子中,func1
期望 3 个参数,但是只有传入了 2 个参数。如果在代码中没有处理这个参数,就很可能出错。
12. 在控制台中快速访问元素
控制台中比querySelector
更快的方法是使用美元符号,$('css-selector')
将返回 CSS 选择器的第一个匹配项。$$('css-selector')
将返回所有匹配项。如果多次使用一个元素,可以把它保存为一个变量。
13. Postman 很棒(但 Firefox 更快)
许多开发人员使用 Postman 查看 ajax 请求。Postman 真的很优秀。但打开一个新的窗口,写入请求对象,然后再来测试它们,显得很麻烦。
有时使用浏览器更容易。
当你使用浏览器查看时,如果请求一个密码验证页面,不需要担心身份验证的 cookie。下面看,在 Firefox 中如何编辑并重新发送请求。
打开控制台并切换到 network 选项卡。右击所需的请求,然后选择编辑并重新发送。现在可以改变任何想要的改的。更改标题并编辑参数,然后点击重新发送。
下面我用不同的属性发起的两次请求:
When debugging JavaScript, Chrome lets you pause when a DOM element changes
14. 中断的节点更改
DOM 是一个有趣的东西。有时候它会改变,你并不知道为什么。 但是,当您调试 JavaScript 时,Chrome 可以在 DOM 元素发生更改时暂停。你甚至可以监视它的属性。在 Chrome 控制台中,右击该元素,然后在设置中选择中断:
编译自:https://raygun.com/javascript-debugging-tips 作者: Luis Alonzo
转载自:http://www.zcfy.cc/article/the-14-javascript-debugging-tips-you-probably-didnt-know-raygun-4480.html 译者: SlaneYang