Skip to content

Commit 4807103

Browse files
lenchen1112ArvinH
andauthored
Debugging in Chrome (#40)
* feat: translation of Debugging in Chrome * Apply suggestions from code review Co-Authored-By: ArvinH <[email protected]>
1 parent a035121 commit 4807103

File tree

1 file changed

+89
-88
lines changed
  • 1-js/03-code-quality/01-debugging-chrome

1 file changed

+89
-88
lines changed

1-js/03-code-quality/01-debugging-chrome/article.md

Lines changed: 89 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,183 +1,184 @@
1-
# Debugging in Chrome
1+
# Chrome 中除錯
22

3-
Before writing more complex code, let's talk about debugging.
3+
在我們寫更複雜的程式碼之前,來說說怎麼除錯吧。
44

5-
[Debugging](https://en.wikipedia.org/wiki/Debugging) is the process of finding and fixing errors within a script. All modern browsers and most other environments support debugging tools -- a special UI in developer tools that makes debugging much easier. It also allows to trace the code step by step to see what exactly is going on.
5+
[除錯(Debugging](https://en.wikipedia.org/wiki/Debugging) 是在腳本中找出並修正錯誤的過程。所有現代瀏覽器和大部分的環境都支援除錯工具 -- 開發工具中特別的使用者介面,用來讓除錯更為簡單。它也可以一步步追蹤程式碼執行步驟,以看出現在到底發生什麼事。
66

7-
We'll be using Chrome here, because it has enough features, most other browsers have a similar process`.
7+
在此我們會使用 Chrome,因為它具有足夠的功能,大部分其它的瀏覽器也有相似的除錯流程。
88

9-
## The "Sources" panel
9+
## "Sources" 面板
1010

11-
Your Chrome version may look a little bit different, but it still should be obvious what's there.
11+
你的 Chrome 版本可能會有些許不同,但它應該還是可以很明顯被看到的。
1212

13-
- Open the [example page](debugging/index.html) in Chrome.
14-
- Turn on developer tools with `key:F12` (Mac: `key:Cmd+Opt+I`).
15-
- Select the `Sources` panel.
13+
- 在 Chrome 中打開 [範例頁面](debugging/index.html)
14+
- 利用 `key:F12`Mac: `key:Cmd+Opt+I`)開啟開發者工具。
15+
- 選取 `Sources` 面板。
1616

17-
Here's what you should see if you are doing it for the first time:
17+
若你是第一次這麼做,你應該會看到這些東西:
1818

1919
![](chrome-open-sources.svg)
2020

21-
The toggler button <span class="devtools" style="background-position:-168px -76px"></span> opens the tab with files.
21+
切換按鈕 <span class="devtools" style="background-position:-168px -76px"></span> 會打開檔案分頁。
2222

23-
Let's click it and select `hello.js` in the tree view. Here's what should show up:
23+
點擊它並在樹狀圖中選擇 `hello.js`,這是應該要顯示的畫面:
2424

2525
![](chrome-tabs.svg)
2626

27-
Here we can see three zones:
27+
在這我們可以看到三個區域:
2828

29-
1. The **Resources zone** lists HTML, JavaScript, CSS and other files, including images that are attached to the page. Chrome extensions may appear here too.
30-
2. The **Source zone** shows the source code.
31-
3. The **Information and control zone** is for debugging, we'll explore it soon.
29+
1. **資源區域** 列出 HTMLJavaScriptCSS 和其他檔案,包含置於頁面中的圖片。Chrome 擴充套件(extensions)也可能顯示在這。
30+
2. **來源區域** 顯示原始碼。
31+
3. **資訊與控制區域** 是為了除錯使用,我們很快會探討它。
3232

33-
Now you could click the same toggler <span class="devtools" style="background-position:-200px -76px"></span> again to hide the resources list and give the code some space.
33+
現在你可以再點擊同一個切換鈕 <span class="devtools" style="background-position:-200px -76px"></span> 來隱藏資源列表來給程式碼多一點空間。
3434

35-
## Console
35+
## 主控台(Console
3636

37-
If we press `key:Esc`, then a console opens below. We can type commands there and press `key:Enter` to execute.
37+
當我們按下 `key:Esc`,則主控台將會在下方開啟。我們可以在此輸入些命令並按下 `key:Enter` 來執行。
3838

39-
After a statement is executed, its result is shown below.
39+
在述語被執行後,其結果會在下方顯示。
4040

41-
For example, here `1+2` results in `3`, and `hello("debugger")` returns nothing, so the result is `undefined`:
41+
例如,這邊的 `1+2` 結果為 `3`,且 `hello("debugger")` 沒回傳東西,所以結果是 `undefined`
4242

4343
![](chrome-sources-console.svg)
4444

45-
## Breakpoints
45+
## 中斷點(Breakpoints
4646

47-
Let's examine what's going on within the code of the [example page](debugging/index.html). In `hello.js`, click at line number `4`. Yes, right on the `4` digit, not on the code.
47+
來檢查看看 [範例頁面](debugging/index.html) 的程式碼發生什麼事。在 `hello.js` 上點擊行號 `4`,沒錯,就是在 `4` 這個數字上點下去,不是點程式碼。
4848

49-
Congratulations! You've set a breakpoint. Please also click on the number for line `8`.
49+
恭喜!你已經設定一個中斷點了。現在也請點擊第 `8` 行的行號。
5050

51-
It should look like this (blue is where you should click):
51+
應該看起來像這樣(藍色是你應該要點的地方):
5252

5353
![](chrome-sources-breakpoint.svg)
5454

55-
A *breakpoint* is a point of code where the debugger will automatically pause the JavaScript execution.
55+
*中斷點* 是除錯器會自動暫停 JavaScript 執行的某個程式碼位置。
5656

57-
While the code is paused, we can examine current variables, execute commands in the console etc. In other words, we can debug it.
57+
當程式碼被暫停,我們可以檢驗目前的變數並在主控台執行一些命令等等。換句話說,我們可以用它來除錯。
5858

59-
We can always find a list of breakpoints in the right panel. That's useful when we have many breakpoints in various files. It allows us to:
60-
- Quickly jump to the breakpoint in the code (by clicking on it in the right panel).
61-
- Temporarily disable the breakpoint by unchecking it.
62-
- Remove the breakpoint by right-clicking and selecting Remove.
63-
- ...And so on.
59+
我們隨時可以在右側面板中找到所有中斷點的列表,當我們在不同檔案間有很多中斷點時會很有用。這允許我們可以:
60+
- 快速跳至程式碼中的某個中斷點(經由在右側面板點擊它)。
61+
- 經由再次點擊來讓某個中斷點暫時失效。
62+
- 右鍵點擊並選擇 Remove 可以移除某個中斷點。
63+
- ...等等。
6464

65-
```smart header="Conditional breakpoints"
66-
*Right click* on the line number allows to create a *conditional* breakpoint. It only triggers when the given expression is truthy.
65+
```smart header="條件式中斷點"
66+
*右鍵點擊* 某行號允許你建立一個 *條件式* 中斷點,它只有在給予的條件為真值時才會被觸發。
6767
68-
That's handy when we need to stop only for a certain variable value or for certain function parameters.
68+
當我們只有在某個特定變數值或特別的函式參數需要中斷時,這會很方便。
6969
```
7070

71-
## Debugger command
71+
## 除錯命令
7272

73-
We can also pause the code by using the `debugger` command in it, like this:
73+
我們也可以經由使用 `debugger` 命令來暫停程式碼,像這樣:
7474

7575
```js
7676
function hello(name) {
7777
let phrase = `Hello, ${name}!`;
7878

7979
*!*
80-
debugger; // <-- the debugger stops here
80+
debugger; // <-- 除錯器會停在這裡
8181
*/!*
8282

8383
say(phrase);
8484
}
8585
```
8686

87-
That's very convenient when we are in a code editor and don't want to switch to the browser and look up the script in developer tools to set the breakpoint.
87+
當我們正在程式碼編輯器中,而不想切換到瀏覽器再到除錯器內查看腳本並設置中斷點時,這會非常方便。
8888

89+
## 暫停且查看
8990

90-
## Pause and look around
91+
在我們的例子中,`hello()` 在頁面載入時被呼叫,所以(在我們設置中斷點後)啟動除錯器最簡單的方法就是重新載入頁面。所以來按下 `key:F5`(Windows, Linux)或 `key:Cmd+R`(Mac)吧。
9192

92-
In our example, `hello()` is called during the page load, so the easiest way to activate the debugger (after we've set the breakpoints) is to reload the page. So let's press `key:F5` (Windows, Linux) or `key:Cmd+R` (Mac).
93-
94-
As the breakpoint is set, the execution pauses at the 4th line:
93+
因為中斷點已經設定了,執行會暫停在第 4 行:
9594

9695
![](chrome-sources-debugger-pause.svg)
9796

98-
Please open the informational dropdowns to the right (labeled with arrows). They allow you to examine the current code state:
97+
請打開右側(箭頭標示)的資訊下拉選單,可以讓你檢查目前的程式碼狀態:
98+
99+
1. **`Watch` -- 顯示目前任何表達式的值。**
99100

100-
1. **`Watch` -- shows current values for any expressions.**
101+
你可以點擊加號 `+` 並輸入表達式,除錯器會隨時顯示它的值,並在程序執行時自動重新計算。
101102

102-
You can click the plus `+` and input an expression. The debugger will show its value at any moment, automatically recalculating it in the process of execution.
103+
2. **`Call Stack` -- 顯示巢狀呼叫鏈。**
103104

104-
2. **`Call Stack` -- shows the nested calls chain.**
105+
此時除錯器正處於 `hello()` 的呼叫之中,被位於 `index.html` 的腳本所呼叫(此處沒有函式,所以顯示為 `anonymous`)。
105106

106-
At the current moment the debugger is inside `hello()` call, called by a script in `index.html` (no function there, so it's called "anonymous").
107+
若你點擊一個堆疊項目(如:"anonymous"),除錯器會跳到其對應的程式碼,且所有它的變數也可以被檢查。
107108

108-
If you click on a stack item (e.g. "anonymous"), the debugger jumps to the corresponding code, and all its variables can be examined as well.
109-
3. **`Scope` -- current variables.**
109+
3. **`Scope` -- 目前的變數。**
110110

111-
`Local` shows local function variables. You can also see their values highlighted right over the source.
111+
`Local` 顯示區域函式變數,你也可以在原始碼中看到它們的值被強調出來。
112112

113-
`Global` has global variables (out of any functions).
113+
`Global` 顯示全域變數(在函式之外)。
114114

115-
There's also `this` keyword there that we didn't study yet, but we'll do that soon.
115+
還有個 `this` 關鍵字我們尚未學習到,但我們很快就會探討它了。
116116

117-
## Tracing the execution
117+
## 追蹤執行
118118

119-
Now it's time to *trace* the script.
119+
現在是時候來 *追蹤* 腳本了。
120120

121-
There are buttons for it at the top of the right panel. Let's engage them.
121+
右側面板頂部有些按鈕可以使用,來用用看吧。
122122

123-
<span class="devtools" style="background-position:-7px -76px"></span> -- continue the execution, hotkey `key:F8`.
124-
: Resumes the execution. If there are no additional breakpoints, then the execution just continues and the debugger loses control.
123+
<span class="devtools" style="background-position:-7px -76px"></span> -- 繼續執行,快捷鍵 `key:F8`
124+
: 恢復執行。若沒有額外的中斷點,則執行將會繼續且除錯器不會再獲取控制。
125125

126-
Here's what we can see after a click on it:
126+
這是我們點下去後會看到的:
127127

128128
![](chrome-sources-debugger-trace-1.svg)
129129

130-
The execution has resumed, reached another breakpoint inside `say()` and paused there. Take a look at the "Call Stack" at the right. It has increased by one more call. We're inside `say()` now.
130+
執行已經被恢復了,直到 `say()` 內的另一個中斷點才暫停。看一下右側 "Call Stack" 的內容,它多增加了一層呼叫,我們現在正在 `say()` 之中。
131131

132-
<span class="devtools" style="background-position:-137px -76px"></span> -- make a step (run the next command), but *don't go into the function*, hotkey `key:F10`.
133-
: If we click it now, `alert` will be shown. The important thing is that `alert` can be any function, the execution "steps over it", skipping the function internals.
132+
<span class="devtools" style="background-position:-137px -76px"></span> -- 下一步(執行下一行命令),但 *不會進入函式*,快捷鍵 `key:F10`
133+
: 若我們點擊它,`alert` 將會顯示出來。要注意這裡的 `alert` 可以是任意函式,執行將會 "跨過(steps over)它",忽略函式的內部。
134134

135-
<span class="devtools" style="background-position:-72px -76px"></span> -- make a step, hotkey `key:F11`.
136-
: The same as the previous one, but "steps into" nested functions. Clicking this will step through all script actions one by one.
135+
<span class="devtools" style="background-position:-72px -76px"></span> -- 下一步,快捷鍵 `key:F11`
136+
: 和前一個相同,但差在會 "跨進(steps into)" 巢狀函式。點擊它將會一步步進行所有腳本的動作。
137137

138-
<span class="devtools" style="background-position:-104px -76px"></span> -- continue the execution till the end of the current function, hotkey `key:Shift+F11`.
139-
: The execution would stop at the very last line of the current function. That's handy when we accidentally entered a nested call using <span class="devtools" style="background-position:-72px -76px"></span>, but it does not interest us, and we want to continue to its end as soon as possible.
138+
<span class="devtools" style="background-position:-104px -76px"></span> -- 繼續執行至目前函式完成為止,快捷鍵 `key:Shift+F11`
139+
: 執行會被暫停在目前函式的最後一行,當我們因使用 <span class="devtools" style="background-position:-72px -76px"></span> 而意外進入不感興趣的巢狀呼叫時會很有用,此時我們只想盡快結束它並接著繼續。
140140

141-
<span class="devtools" style="background-position:-7px -28px"></span> -- enable/disable all breakpoints.
142-
: That button does not move the execution. Just a mass on/off for breakpoints.
141+
<span class="devtools" style="background-position:-7px -28px"></span> -- 啟用/關閉 所有中斷點。
142+
: 此按鈕不影響執行,只會讓中斷點全開/關。
143143

144-
<span class="devtools" style="background-position:-264px -4px"></span> -- enable/disable automatic pause in case of an error.
145-
: When enabled, and the developer tools is open, a script error automatically pauses the execution. Then we can analyze variables to see what went wrong. So if our script dies with an error, we can open debugger, enable this option and reload the page to see where it dies and what's the context at that moment.
144+
<span class="devtools" style="background-position:-264px -4px"></span> -- 啟用/關閉 遇到錯誤時的自動暫停功能。
145+
: 當啟用且開發者工具開著時,腳本錯誤將會自動暫停執行,然後我們可以分析變數來看什麼東西壞了。所以若腳本因為錯誤而掛點,我們可以打開除錯器,啟用這個選項並重新載入頁面來看看它掛在哪和當下的週遭環境是什麼。
146146

147-
```smart header="Continue to here"
148-
Right click on a line of code opens the context menu with a great option called "Continue to here".
147+
```smart header="繼續至此(Continue to here"
148+
右鍵點擊一行程式碼可以開啟有著一個非常棒選項的選單,"Continue to here"
149149
150-
That's handy when we want to move multiple steps forward to the line, but we're too lazy to set a breakpoint.
150+
當我們想要向前移動多個步驟直到此行,卻又懶到不想設置中斷點時,這會非常方便。
151151
```
152152

153-
## Logging
153+
## 紀錄
154154

155-
To output something to console from our code, there's `console.log` function.
155+
想要從我們的程式碼輸出某些東西到主控台時,有個 `console.log` 函式可以用。
156156

157-
For instance, this outputs values from `0` to `4` to console:
157+
舉個例,這將會輸出從 `0` `4` 的值到主控台:
158158

159159
```js run
160-
// open console to see
160+
// 打開主控台來看看
161161
for (let i = 0; i < 5; i++) {
162162
console.log("value,", i);
163163
}
164164
```
165165

166-
Regular users don't see that output, it is in the console. To see it, either open the Console panel of developer tools or press `key:Esc` while in another panel: that opens the console at the bottom.
166+
一般使用者不會看到這些輸出,它只在主控台內。想要看到的話,只能打開開發者工具的主控台面板,或在位於其他面板時按下 `key:Ese`:這將在底部開啟主控台。
167+
168+
若我們在程式碼內做充足的紀錄,則我們可以從紀錄中看發生什麼事,而不需要用到除錯器。
167169

168-
If we have enough logging in our code, then we can see what's going on from the records, without the debugger.
170+
## 總結
169171

170-
## Summary
172+
如我們所見,有三種主要暫停腳本的方式:
173+
1. 中斷點。
174+
2. `debugger` 述語。
175+
3. 錯誤(若開發者工具開著且按鈕 <span class="devtools" style="background-position:-264px -4px"></span> 開啟著)。
171176

172-
As we can see, there are three main ways to pause a script:
173-
1. A breakpoint.
174-
2. The `debugger` statements.
175-
3. An error (if dev tools are open and the button <span class="devtools" style="background-position:-264px -4px"></span> is "on").
177+
當暫停時,我們可以除錯 - 檢查變數和追蹤程式碼來看執行哪裡有問題。
176178

177-
When paused, we can debug - examine variables and trace the code to see where the execution goes wrong.
179+
開發者工具有更多尚未在此被介紹的選項,完整的操作手冊在 <https://developers.google.com/web/tools/chrome-devtools>
178180

179-
There are many more options in developer tools than covered here. The full manual is at <https://developers.google.com/web/tools/chrome-devtools>.
181+
本章節的資訊已經夠用於除錯了,但之後,尤其當你做了很多瀏覽器相關的事以後,請去看看更多開發者工具的進階功能。
180182

181-
The information from this chapter is enough to begin debugging, but later, especially if you do a lot of browser stuff, please go there and look through more advanced capabilities of developer tools.
183+
喔,當然你也可以點擊開發者工具的不同位置來看看什麼會顯示出來,這也許是學習使用開發者工具最快的途徑,也不要忘了右鍵點擊查看選單喔!
182184

183-
Oh, and also you can click at various places of dev tools and just see what's showing up. That's probably the fastest route to learn dev tools. Don't forget about the right click and context menus!

0 commit comments

Comments
 (0)