From 218934fa2ed1c702a6d3923d2aa2cc6b43c48684 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:43:23 -0500 Subject: initial commit --- files/zh-tw/glossary/callback_function/index.html | 36 +++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 files/zh-tw/glossary/callback_function/index.html (limited to 'files/zh-tw/glossary/callback_function') diff --git a/files/zh-tw/glossary/callback_function/index.html b/files/zh-tw/glossary/callback_function/index.html new file mode 100644 index 0000000000..3bfcdf6dd3 --- /dev/null +++ b/files/zh-tw/glossary/callback_function/index.html @@ -0,0 +1,36 @@ +--- +title: 回呼函式 +slug: Glossary/Callback_function +translation_of: Glossary/Callback_function +--- +

回呼函式(callback function)是指能藉由參數(argument)通往另一個函式的函式。它會在外部函式內調用、以完成某些事情。

+ +

以下是簡單的範例:

+ +
function greeting(name) {
+  alert('Hello ' + name);
+}
+
+function processUserInput(callback) {
+  var name = prompt('輸入你的名字:');
+  callback(name);
+}
+
+processUserInput(greeting);
+ +

這是個{{glossary("synchronous","同步")}}回呼的例子,因為它是立即執行的。

+ +

但請注意,回呼常用來延續{{glossary("asynchronous","非同步")}}行動完成後的程式執行:這就叫做非同步回呼(asynchronous callbacks)。例如說我們的 maps-example.html 範例(也請參見這個)用了 Google Maps API 與 Geolocation API 來把你設備的位置,顯示到地圖上。

+ +

因為我們透過非同步取得 GPS 的設備坐標(我們不知道數據何時會被回傳),{{domxref("Geolocation.getCurrentPosition()")}} 方法把一個匿名回呼函式作為參數,它會回傳一個坐標數據以充當參數。該函式會在回傳坐標數據後執行。

+ +

深入理解

+ +

一般常識

+ + -- cgit v1.2.3-54-g00ecf