-
Notifications
You must be signed in to change notification settings - Fork 22
Introduction: callbacks #101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,15 +1,15 @@ | ||||||
|
|
||||||
| # Animated circle with callback | ||||||
| # 使用回呼的動畫圈圈 | ||||||
|
|
||||||
| In the task <info:task/animate-circle> an animated growing circle is shown. | ||||||
| 在作業 <info:task/animate-circle> 中,成長的圈圈以動畫呈現。 | ||||||
|
|
||||||
| Now let's say we need not just a circle, but to show a message inside it. The message should appear *after* the animation is complete (the circle is fully grown), otherwise it would look ugly. | ||||||
| 現在,假設我們需要的不只是圈圈,還要在裡面顯示訊息。訊息應該要出現在動畫完成(圈圈完全地成長)*之後*,否則它看起來會很醜。 | ||||||
|
|
||||||
| In the solution of the task, the function `showCircle(cx, cy, radius)` draws the circle, but gives no way to track when it's ready. | ||||||
| 在該項作業的解答中,函式 `showCircle(cx, cy, radius)` 畫了圈圈,但沒有提供追蹤它完成的方法。 | ||||||
|
||||||
| 在該項作業的解答中,函式 `showCircle(cx, cy, radius)` 畫了圈圈,但沒有提供追蹤它完成的方法。 | |
| 在該項課題的解答中,函式 `showCircle(cx, cy, radius)` 畫了圈圈,但沒有提供追蹤它完成的方法。 |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 增加一個回呼參數:`showCircle(cx, cy, radius, callback)` 讓它在動畫完成後被呼叫。 `callback` 應該要接收圈圈的 `<div>` 作為參數。 | |
| 增加一個回呼參數:`showCircle(cx, cy, radius, callback)` 讓它在動畫完成後被呼叫。 `callback` 應該要接收圈圈的 `<div>` 作為引數。 |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 範例: | |
| 演示: |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 將該作業的解答 <info:task/animate-circle> 當作基底。 | |
| 將該課題的解答 <info:task/animate-circle> 當作基底。 |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,68 +1,67 @@ | ||||||||||
|
|
||||||||||
|
|
||||||||||
| # Introduction: callbacks | ||||||||||
| # 介紹: 回呼 | ||||||||||
|
|
||||||||||
| ```warn header="We use browser methods in examples here" | ||||||||||
| To demonstrate the use of callbacks, promises and other abstract concepts, we'll be using some browser methods: specifically, loading scripts and performing simple document manipulations. | ||||||||||
| 為了示範回呼、promise 及其他抽象概念的使用,我們將會使用一些瀏覽器的函式:更具體地說,載入腳本以及執行簡單的文件操作。 | ||||||||||
|
|
||||||||||
| If you're not familiar with these methods, and their usage in the examples is confusing, you may want to read a few chapters from the [next part](/document) of the tutorial. | ||||||||||
| 如果你並不熟悉這些方法,亦或是對於範例中的使用方式感到困惑,你可能會想要閱讀[下一部分](/document)教程中的一些章節。 | ||||||||||
|
|
||||||||||
| Although, we'll try to make things clear anyway. There won't be anything really complex browser-wise. | ||||||||||
| 我們會試著讓事情保持單純。不會有任何瀏覽器方面的複雜事物。 | ||||||||||
|
||||||||||
| 我們會試著讓事情保持單純。不會有任何瀏覽器方面的複雜事物。 | |
| 儘管如此,我們還是會試著讓事情保持單純。不會有任何瀏覽器方面的複雜事物。 |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| // 建立一個 <script> 標記,然後將它加到頁面上 | |
| // 建立一個 <script> 標籤,然後將它加到頁面上 |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 這新的、動態生成的標記 `<script src="…">` 加到文件中。瀏覽器自動地開始載入它,並在載入完成後執行它。 | |
| 這新的、動態生成的標籤 `<script src="…">` 將從給予的 `src` 加到文件中。瀏覽器自動地開始載入它,並在載入完成後執行它。 |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 腳本以〝非同步〞的方式被執行,因為它現在開始被載入,但之後才會執行。要注意的是在載入開始,到腳本執行之前,這個函式已經執行完畢了。 | |
| 腳本以〝非同步〞的方式被執行,因為它現在開始被載入,但在函式已結束之後才會執行。。 |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 假設我們想要在腳本載入完成後,馬上使用它。腳本宣告了新的函式,而我們想要執行它們。 | |
| 倘若我們想要在腳本載入完成後馬上使用它。也就是腳本宣告了新的函式,而我們想要執行它們。 |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 這是因為,瀏覽器沒有足夠的時間載入腳本。現在, `loadScript` 函式沒有提供某種方式來追蹤載入完成了沒。我們只能知道,腳本終究會載入,然後執行。但是我們想要在腳本載入完成後,使用來自該腳本的新函式及新變數。 | |
| 顯然,瀏覽器應該沒有足夠的時間載入腳本。至此, `loadScript` 函式尚未提供方式來追蹤載入完成了沒。我們只知道,腳本終究會載入並執行。但我們希望知道完成的時機點,以便使用來自該腳本的新函式及新變數。 |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 這被稱為〝基於回呼〞風格的非同步程式設計。執行某些非同步動作的函式,應該要提供一個 `callback` 參數,讓我們能在非同步函式完成時,執行我們傳入的回呼。 | |
| 這被稱為〝基於回呼(callback-based)〞風格的非同步程式設計。執行某些非同步動作的函式,應該要提供一個 `callback` 引數,讓我們能在非同步函式完成時,執行我們傳入的回呼。 |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 如果我們想在多一個腳本呢‧‧‧? | |
| 如果我們想再多一個腳本呢‧‧‧? |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 所以,每一個新動作都在一個回呼內。在只有少數動作的情況下,這是沒問題的。但如果有很多動作的話,那就不好了。我們很快會看到其它變型。 | |
| 所以,每一個新動作都在一個回呼內。在只有少數動作的情況下還好。但如果有很多動作的話,那就不好了。我們很快會看到其它變型。 |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 再強調一次,我們使用在 `loadScript` 的處理方式,其實相當常見。它被稱為〝錯誤優先回呼〞的風格。 | |
| 再強調一次,我們使用在 `loadScript` 的處理方式,其實相當常見。它被稱為〝錯誤優先回呼(error-first callback)〞的風格。 |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 1. `callback` 的第一個參數保留給錯誤,如果它有發生的話。然後呼叫 `callback(err)`。 | |
| 2. 第二個參數(以及之後的其它參數)保留給成功的結果。然後呼叫 `callback(null, result1, result2…)`。 | |
| 1. `callback` 的第一個引數保留給錯誤,如果它有發生的話。然後呼叫 `callback(err)`。 | |
| 2. 第二個引數(以及之後的其它引數)保留給成功的結果。然後呼叫 `callback(null, result1, result2…)`。 |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 所以單一的 `callback` 函式,被用於錯誤回報以及回傳結果。 | |
| 所以單一的 `callback` 函式,被同時用於錯誤回報以及回傳結果。 |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 第一眼看來,對於非同步程式設計來說,上述的方式是可行的。而它確實也是可行的。對於一到二層的巢狀呼叫來說,看起來還不錯。 | |
| 第一眼看來,對於非同步程式設計來說,上述的方式是可行的。而它確實也是可行的。對於一到二層的巢狀呼叫來說,看起來還行。 |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 但是對於多個非同步動作,一個接著一個,我們將會有像這樣的程式碼: | |
| 但是對於一個接著一個的多個非同步動作,我們將會有像這樣的程式碼: |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 隨著呼叫的層次越多,程式碼變得越來越深,同時也增加了維護的難度,尤其是實際的程式碼中可能會有更多的迴圈、條件判斷等等。而不是像範例中的 `...` 。 | |
| 隨著呼叫的層次越多,程式碼變得越來越深,同時也增加了維護的難度,尤其是實際的程式碼中可能會有更多的迴圈、條件判斷等等。而不只是範例中的 `...` 。 |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 這有時候被稱為〝回呼地獄(callback hell)〞或〝金字塔的詛咒〞。 | |
| 這有時候被稱為〝回呼地獄(callback hell)〞或〝金字塔的詛咒(pyramid of doom)〞。 |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 這能運作,但程式碼看起來像被撕破的草稿。它很難閱讀,而且你大概也注意到了,讀者需要在閱讀時,在片段間做視線的跳躍。這很不方便,尤其是當讀者並不熟悉這段程式碼,而且不曉得視線要跳到哪裡。 | |
| 這雖然可行,但程式碼看起來像被撕破的草稿。它很難閱讀,而且你大概也注意到了,讀者需要在閱讀時,在片段間做視線的跳躍。這很不方便,尤其是當讀者並不熟悉這段程式碼,而且不曉得視線要跳到哪裡。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.