From 218934fa2ed1c702a6d3923d2aa2cc6b43c48684 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:43:23 -0500 Subject: initial commit --- .../reference/errors/not_a_function/index.html | 80 ++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 files/zh-tw/web/javascript/reference/errors/not_a_function/index.html (limited to 'files/zh-tw/web/javascript/reference/errors/not_a_function') diff --git a/files/zh-tw/web/javascript/reference/errors/not_a_function/index.html b/files/zh-tw/web/javascript/reference/errors/not_a_function/index.html new file mode 100644 index 0000000000..24ce79a6e4 --- /dev/null +++ b/files/zh-tw/web/javascript/reference/errors/not_a_function/index.html @@ -0,0 +1,80 @@ +--- +title: 'TypeError: "x" is not a function' +slug: Web/JavaScript/Reference/Errors/Not_a_function +translation_of: Web/JavaScript/Reference/Errors/Not_a_function +--- +
{{jsSidebar("Errors")}}
+ +

訊息

+ +
TypeError: "x" is not a function
+
+ +

錯誤類型

+ +

{{jsxref("TypeError")}}.

+ +

哪裡錯了?

+ +

你想以函式呼叫一個數值,但該數值其實不是函式。程式碼期望你給出函式,但這份期望落空了。

+ +

也許打錯了函式的名字?也許呼叫的物件並沒有這個函式?例如說 JavaScript 物件並沒有 map 函式,但 JavaScript Array(陣列)物件則有。

+ +

許多內建函式都需要回呼(callback)的函式。為了讓下面的方法順利運作,你需要為它們提供函式:

+ + + +

實例

+ +

函式的名字打錯了

+ +

這種事太常發生了。下例就有個方法打錯:

+ +
var x = document.getElementByID("foo");
+// TypeError: document.getElementByID is not a function
+
+ +

該函式的正確名字為 getElementById

+ +
var x = document.getElementById("foo");
+
+ +

函式呼叫到錯誤的物件

+ +

某些方法需要你提供回呼的函式,該函式只能作用於特定物件。以本例而言,我們使用的 {{jsxref("Array.prototype.map()")}} 就只能作用於 {{jsxref("Array")}} 物件。

+ +
var obj = { a: 13, b: 37, c: 42 };
+
+obj.map(function(num) {
+  return num * 2;
+});
+
+// TypeError: obj.map is not a function
+ +

請改用陣列:

+ +
var numbers = [1, 4, 9];
+
+numbers.map(function(num) {
+  return num * 2;
+});
+
+// Array [ 2, 8, 18 ]
+
+ +

參見

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