调试基础
学习 practical 调试技巧 — print 调试, 读取 stack traces, and forming hypotheses 到 fix bugs.
你将学到
- 什么调试是 and mindset 后面它
- 如何 use console.log 到 inspect 值
- 如何 读取 stack trace 到 locate bug
- hypothesis-测试 cycle for fixing bugs systematically
概念讲解
Debugging Basics
Debugging is the process of finding and fixing bugs. A bug is any difference between what your program does and what you intended. Debugging is not a separate activity from programming — it is most of programming. Becoming good at it is what makes you a productive developer.
The Debugging Mindset
Debugging is detective work. You observe a symptom (wrong output, a crash) and work backward to the cause. The key attitudes are:
- Curiosity — "为什么做它做那个?" not "它应该工作!"
- Patience — bugs are often subtle; rushing leads to wrong fixes.
- Evidence over assumption — verify your beliefs with the program's actual behavior.
Print Debugging with console.log
The simplest technique: add console.log statements to print the value of variables at key points. This reveals what the program is *actually* doing, step by step.
function total(cart) {
console.log("cart:", cart); // what came in?
let sum = 0;
for (const item of cart) {
console.log("项:", item, "总和所以远:", sum);
sum += item.price;
}
console.log("最终总和:", sum);
return sum;
}
Reading a Stack Trace
When an error occurs, the stack trace shows the chain of function calls that led to it. Read it top to bottom: the first frame is where the error happened; the frames below are who called whom. This pinpoints the location.
The Hypothesis-Test Cycle
- Reproduce the bug reliably.
- Form a hypothesis — "I think 循环运行 one extra 时间."
- Test it — add a log or change the code to confirm.
- Fix — if the hypothesis is confirmed, make the targeted change.
- Verify — confirm the fix works and does not break other things.
Avoid "random 更改 and hope" — each fix should test a specific hypothesis. This turns debugging from a frustrating guessing game into a methodical process.
示例代码
// Buggy function: should average a list of numbers
function average(numbers) {
let sum = 0;
for (let i = 0; i <= numbers.length; i++) { // BUG: <= should be <
sum += numbers[i];
}
return sum / numbers.length;
}
// Debugging: log each step
function averageDebug(numbers) {
let sum = 0;
for (let i = 0; i < numbers.length; i++) {
console.log(`i=${i}, value=${numbers[i]}, sum=${sum}`);
sum += numbers[i];
}
console.log(`final sum=${sum}, count=${numbers.length}`);
return sum / numbers.length;
}
console.log(averageDebug([10, 20, 30])); // 20 — correct!
首先 version 有 off-通过-one 错误 (<= 相反的 <), 哪个添加 undefined 到总和 and produces NaN. 调试 version logs 每个步骤, 制作边界错误明显.
动手试一试
- JSON Diff
Diffing 是调试技巧: compare expected 对比. 实际输出到 pinpoint 精确地哪里它们 differ.
常见错误
错误写法
更改 code randomly hoping bug disappears.
正确做法
表单 hypothesis 首先, 然后测试它 使用 log or 小更改. Random 更改经常 introduce 新 bugs 不使用 fixing 原始.
错误写法
Leaving 调试 console.log 语句 in finished code.
正确做法
移除 or gate 它们后面调试 flag 之前 shipping. Stray logs clutter console and 能 leak sensitive 数据 in production.
相关资源
相关代码片段
- JavaScript 数组方法
理解构建-in 方法 reduces bugs 你编写通过 hand.
相关速查表
- JavaScript Cheatsheet
引用 for console 方法 and 错误 handling 语法.
练习 调试基础
3 个练习
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。