From 218934fa2ed1c702a6d3923d2aa2cc6b43c48684 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:43:23 -0500 Subject: initial commit --- .../errors/bad_return_or_yield/index.html | 51 ++++++++++++++ .../web/javascript/reference/errors/index.html | 22 ++++++ .../errors/invalid_array_length/index.html | 74 ++++++++++++++++++++ .../missing_curly_after_property_list/index.html | 47 +++++++++++++ .../reference/errors/no_properties/index.html | 36 ++++++++++ .../reference/errors/not_a_function/index.html | 80 ++++++++++++++++++++++ .../reference/errors/not_defined/index.html | 67 ++++++++++++++++++ .../errors/redeclared_parameter/index.html | 57 +++++++++++++++ .../reference/errors/too_much_recursion/index.html | 50 ++++++++++++++ .../reference/errors/unexpected_type/index.html | 49 +++++++++++++ 10 files changed, 533 insertions(+) create mode 100644 files/zh-tw/web/javascript/reference/errors/bad_return_or_yield/index.html create mode 100644 files/zh-tw/web/javascript/reference/errors/index.html create mode 100644 files/zh-tw/web/javascript/reference/errors/invalid_array_length/index.html create mode 100644 files/zh-tw/web/javascript/reference/errors/missing_curly_after_property_list/index.html create mode 100644 files/zh-tw/web/javascript/reference/errors/no_properties/index.html create mode 100644 files/zh-tw/web/javascript/reference/errors/not_a_function/index.html create mode 100644 files/zh-tw/web/javascript/reference/errors/not_defined/index.html create mode 100644 files/zh-tw/web/javascript/reference/errors/redeclared_parameter/index.html create mode 100644 files/zh-tw/web/javascript/reference/errors/too_much_recursion/index.html create mode 100644 files/zh-tw/web/javascript/reference/errors/unexpected_type/index.html (limited to 'files/zh-tw/web/javascript/reference/errors') diff --git a/files/zh-tw/web/javascript/reference/errors/bad_return_or_yield/index.html b/files/zh-tw/web/javascript/reference/errors/bad_return_or_yield/index.html new file mode 100644 index 0000000000..7a28fb4be3 --- /dev/null +++ b/files/zh-tw/web/javascript/reference/errors/bad_return_or_yield/index.html @@ -0,0 +1,51 @@ +--- +title: 'SyntaxError: return not in function' +slug: Web/JavaScript/Reference/Errors/Bad_return_or_yield +translation_of: Web/JavaScript/Reference/Errors/Bad_return_or_yield +--- +
{{jsSidebar("Errors")}}
+ +

訊息

+ +
SyntaxError: return not in function
+SyntaxError: yield not in function
+
+ +

錯誤類型

+ +

{{jsxref("SyntaxError")}}

+ +

哪裡錯了?

+ +

returnyield 宣告在函式以外的地方被呼叫。也許少寫了一個大括號?returnyield 宣告必須要寫在函式裡面,因為它們結束(或暫停並恢復)函式的執行,並且回傳了特定值。

+ +

實例

+ +
var cheer = function(score) {
+  if (score === 147)
+    return "Maximum!";
+  };
+  if (score > 100) {
+    return "Century!";
+  }
+}
+
+// SyntaxError: return not in function
+ +

乍看之下大括號寫對了,但其實在第一個 if 的後面,少了一個 {。正確的寫法應該是:

+ +
var cheer = function(score) {
+  if (score === 147) {
+    return "Maximum!";
+  }
+  if (score > 100) {
+    return "Century!";
+  }
+};
+ +

參見

+ + diff --git a/files/zh-tw/web/javascript/reference/errors/index.html b/files/zh-tw/web/javascript/reference/errors/index.html new file mode 100644 index 0000000000..8f553faa75 --- /dev/null +++ b/files/zh-tw/web/javascript/reference/errors/index.html @@ -0,0 +1,22 @@ +--- +title: JavaScript 錯誤參考資料 +slug: Web/JavaScript/Reference/Errors +translation_of: Web/JavaScript/Reference/Errors +--- +

{{jsSidebar("Errors")}}

+ +

在這裡,你可以看到一些由 JavaScript 拋出的錯誤一覽。這些錯誤訊息對你的除錯很有幫助,但拋出的錯誤也不是每次都能講清楚。本頁回提供這些錯誤的詳細資訊。所有的錯誤都是由 {{jsxref("Error")}} 物件所建立的物件,有著 namemessage

+ +

錯誤會出現在網路主控台、可能還連接到相應頁面,以幫助你儘速理解程式碼裡面的問題。

+ +

錯誤一覽表

+ +

In this list, each page is listed by name (the type of error) and message (a more detailed human-readable error message). Together, these two properties provide a starting point toward understanding and resolving the error. For more information, follow the links below!

+ +

{{ListSubPages("/zh-TW/docs/Web/JavaScript/Reference/Errors")}}

+ +

參見

+ + diff --git a/files/zh-tw/web/javascript/reference/errors/invalid_array_length/index.html b/files/zh-tw/web/javascript/reference/errors/invalid_array_length/index.html new file mode 100644 index 0000000000..ee2c5f08e4 --- /dev/null +++ b/files/zh-tw/web/javascript/reference/errors/invalid_array_length/index.html @@ -0,0 +1,74 @@ +--- +title: 'RangeError: invalid array length' +slug: Web/JavaScript/Reference/Errors/Invalid_array_length +translation_of: Web/JavaScript/Reference/Errors/Invalid_array_length +--- +
{{jsSidebar("Errors")}}
+ +

訊息

+ +
RangeError: Array length must be a finite positive integer (Edge)
+RangeError: invalid array length (Firefox)
+RangeError: Invalid array length (Chrome)
+RangeError: Invalid array buffer length (Chrome)
+
+ +

錯誤類型

+ +

{{jsxref("RangeError")}}

+ +

哪裡錯了?

+ +

一個無效的陣列長度可能發生於以下幾種情形:

+ + + +

為什麼 Array  和 ArrayBuffer 的長度有限?   Array 和 ArrayBuffer 的屬性以一個32位元的非負整數表使,因此僅能儲存 0 到 232-1 的數值。

+ +

If you are creating an Array, using the constructor, you probably want to use the literal notation instead, as the first argument is interpreted as the length of the Array.

+ +

Otherwise, you might want to clamp the length before setting the length property, or using it as argument of the constructor.

+ +

示例

+ +

無效的案例

+ +
new Array(Math.pow(2, 40))
+new Array(-1)
+new ArrayBuffer(Math.pow(2, 32))
+new ArrayBuffer(-1)
+
+let a = [];
+a.length = a.length - 1;         // set -1 to the length property
+
+let b = new Array(Math.pow(2, 32) - 1);
+b.length = b.length + 1;         // set 2^32 to the length property
+
+ +

有效的案例

+ +
[ Math.pow(2, 40) ]                     // [ 1099511627776 ]
+[ -1 ]                                  // [ -1 ]
+new ArrayBuffer(Math.pow(2, 32) - 1)
+new ArrayBuffer(0)
+
+let a = [];
+a.length = Math.max(0, a.length - 1);
+
+let b = new Array(Math.pow(2, 32) - 1);
+b.length = Math.min(0xffffffff, b.length + 1);
+
+// 0xffffffff 是 2^32 - 1 的十六進位表示
+// 也可以寫成 (-1 >>> 0)
+
+ +

參見

+ + diff --git a/files/zh-tw/web/javascript/reference/errors/missing_curly_after_property_list/index.html b/files/zh-tw/web/javascript/reference/errors/missing_curly_after_property_list/index.html new file mode 100644 index 0000000000..7e3728fc49 --- /dev/null +++ b/files/zh-tw/web/javascript/reference/errors/missing_curly_after_property_list/index.html @@ -0,0 +1,47 @@ +--- +title: 'SyntaxError: missing } after property list' +slug: Web/JavaScript/Reference/Errors/Missing_curly_after_property_list +translation_of: Web/JavaScript/Reference/Errors/Missing_curly_after_property_list +--- +
{{jsSidebar("Errors")}}
+ +

訊息

+ +
SyntaxError: missing } after property list
+
+ +

錯誤類型

+ +

{{jsxref("SyntaxError")}}

+ +

何處出錯?

+ +

在物件初始化時,語法錯誤。  實際上可能遺漏一個大括號或是逗號。 例如, 同時檢查大括弧以及逗號是否以正確的順序關閉。 縮排或是有規則的排序代碼是有幫助您找出複雜的代碼錯誤。

+ +

範例

+ +

忘記逗號

+ +

有時候,在初始化物件時,缺少一個逗號:

+ +
var obj = {
+  a: 1,
+  b: { myProp: 2 }
+  c: 3
+};
+
+ +

Correct would be:

+ +
var obj = {
+  a: 1,
+  b: { myProp: 2 },
+  c: 3
+};
+
+ +

參見

+ + diff --git a/files/zh-tw/web/javascript/reference/errors/no_properties/index.html b/files/zh-tw/web/javascript/reference/errors/no_properties/index.html new file mode 100644 index 0000000000..b355d15ea3 --- /dev/null +++ b/files/zh-tw/web/javascript/reference/errors/no_properties/index.html @@ -0,0 +1,36 @@ +--- +title: 'TypeError: "x" has no properties' +slug: Web/JavaScript/Reference/Errors/No_properties +translation_of: Web/JavaScript/Reference/Errors/No_properties +--- +
{{jsSidebar("Errors")}}
+ +

訊息

+ +
TypeError: null has no properties
+TypeError: undefined has no properties
+
+ +

錯誤類型

+ +

{{jsxref("TypeError")}}.

+ +

哪裡錯了?

+ +

{{jsxref("null")}} 與 {{jsxref("undefined")}} 並沒有可訪問的屬性。

+ +

示例

+ +
null.foo;
+// TypeError: null has no properties
+
+undefined.bar;
+// TypeError: undefined has no properties
+
+ +

參見

+ + 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 ]
+
+ +

參見

+ + diff --git a/files/zh-tw/web/javascript/reference/errors/not_defined/index.html b/files/zh-tw/web/javascript/reference/errors/not_defined/index.html new file mode 100644 index 0000000000..fa79033977 --- /dev/null +++ b/files/zh-tw/web/javascript/reference/errors/not_defined/index.html @@ -0,0 +1,67 @@ +--- +title: 'ReferenceError: "x" is not defined' +slug: Web/JavaScript/Reference/Errors/Not_defined +translation_of: Web/JavaScript/Reference/Errors/Not_defined +--- +
{{jsSidebar("Errors")}}
+ +

訊息

+ +
ReferenceError: "x" is not defined
+
+ +

錯誤類型

+ +

{{jsxref("ReferenceError")}}.

+ +

哪裡錯了?

+ +

有個地方參照到不存在的變數了。這個變數需要宣告、或確定在目前腳本、或在 {{Glossary("scope")}} 裡可用。

+ +
+

注意:如果要使用函式庫(例如 jQuery)的話,請確定在你使用諸如 $ 這樣的函式庫變數前,就已載入完畢。把載入函式庫的 {{HTMLElement("script")}} 標籤,放在你使用的程式碼之前。 +

+
+ +

實例

+ +

變數未宣告

+ +
foo.substring(1); // ReferenceError: foo is not defined
+
+ +

"foo" 變數在任何地方都沒被定義到。它需要字串使 {{jsxref("String.prototype.substring()")}} 得以運作。

+ +
var foo = "bar";
+foo.substring(1); // "ar"
+ +

作用域錯誤

+ +

A variable need to be available in the current context of execution. Variables defined inside a function cannot be accessed from anywhere outside the function, because the variable is defined only in the scope of the function

+ +
function numbers () {
+  var num1 = 2,
+      num2 = 3;
+  return num1 + num2;
+}
+
+console.log(num1); // ReferenceError num1 is not defined.
+ +

However, a function can access all variables and functions defined inside the scope in which it is defined. In other words, a function defined in the global scope can access all variables defined in the global scope.

+ +
var num1 = 2,
+    num2 = 3;
+
+function numbers () {
+  return num1 + num2;
+}
+
+console.log(num1); // 2
+ +

參閱

+ + diff --git a/files/zh-tw/web/javascript/reference/errors/redeclared_parameter/index.html b/files/zh-tw/web/javascript/reference/errors/redeclared_parameter/index.html new file mode 100644 index 0000000000..e9ba8cbbe0 --- /dev/null +++ b/files/zh-tw/web/javascript/reference/errors/redeclared_parameter/index.html @@ -0,0 +1,57 @@ +--- +title: 'SyntaxError: redeclaration of formal parameter "x"' +slug: Web/JavaScript/Reference/Errors/Redeclared_parameter +translation_of: Web/JavaScript/Reference/Errors/Redeclared_parameter +--- +
{{jsSidebar("Errors")}}
+ +

訊息

+ +
SyntaxError: redeclaration of formal parameter "x" (Firefox)
+SyntaxError: Identifier "x" has already been declared (Chrome)
+
+ +

錯誤類型

+ +

{{jsxref("SyntaxError")}}

+ +

哪裡錯了?

+ +

當相同的變數名作為函式的參數、接著又在函式體(function body)內用了 let 重複宣告並指派時出現。在 JavaScript 裡面,不允許在相同的函式、或是作用域區塊(block scope)內重複宣告相同的 let 變數。

+ +

實例

+ +

在這裡,「arg」變數的參數被重複宣告。

+ +
function f(arg) {
+  let arg = 'foo';
+}
+
+// SyntaxError: redeclaration of formal parameter "arg"
+
+ +

If you want to change the value of "arg" in the function body, you can do so, but you do not need to declare the same variable again. In other words: you can omit the let keyword. If you want to create a new variable, you need to rename it as conflicts with the function parameter already.

+ +
function f(arg) {
+  arg = 'foo';
+}
+
+function f(arg) {
+  let bar = 'foo';
+}
+
+ +

相容性註解

+ + + +

參見

+ + diff --git a/files/zh-tw/web/javascript/reference/errors/too_much_recursion/index.html b/files/zh-tw/web/javascript/reference/errors/too_much_recursion/index.html new file mode 100644 index 0000000000..1708683ffa --- /dev/null +++ b/files/zh-tw/web/javascript/reference/errors/too_much_recursion/index.html @@ -0,0 +1,50 @@ +--- +title: 'InternalError: too much recursion' +slug: Web/JavaScript/Reference/Errors/Too_much_recursion +translation_of: Web/JavaScript/Reference/Errors/Too_much_recursion +--- +
{{jsSidebar("Errors")}}
+ +

訊息

+ +
InternalError: too much recursion
+
+ +

錯誤類型

+ +

{{jsxref("InternalError")}}

+ +

哪裡錯了?

+ +

一個呼叫自己的函式稱為遞迴函式(recursive function)。在某些方面,遞迴和迴圈很像。它們都需要在指定條件(以避免無窮迴圈,或是本例的無窮遞迴)下,重複執行數次相同的程式碼。如果遞迴執行太多次、或成為無窮遞迴的話,JavaScript 就會出現這個錯誤。

+ +

實例

+ +

以下的遞迴函式,會根據終止條件,而運行十次。

+ +
function loop(x) {
+  if (x >= 10) // "x >= 10" 是終止條件
+    return;
+  // do stuff
+  loop(x + 1); // 遞迴呼叫
+}
+loop(0);
+ +

如果把終止條件的次數設得太高,函式就不會運作了:

+ +
function loop(x) {
+  if (x >= 1000000000000)
+    return;
+  // do stuff
+  loop(x + 1);
+}
+loop(0);
+
+// InternalError: too much recursion
+ +

參見

+ + diff --git a/files/zh-tw/web/javascript/reference/errors/unexpected_type/index.html b/files/zh-tw/web/javascript/reference/errors/unexpected_type/index.html new file mode 100644 index 0000000000..d7399baf5d --- /dev/null +++ b/files/zh-tw/web/javascript/reference/errors/unexpected_type/index.html @@ -0,0 +1,49 @@ +--- +title: 'TypeError: "x" is (not) "y"' +slug: Web/JavaScript/Reference/Errors/Unexpected_type +translation_of: Web/JavaScript/Reference/Errors/Unexpected_type +--- +

錯誤類型

+ +

{{jsxref("TypeError")}}

+ +

哪裡錯了?

+ +

有一個意想不到的類型。這與 {{jsxref("undefined")}} 或 {{jsxref("null")}} 值經常發生。

+ +

另外,某些方法,如 {{jsxref("Object.create()")}} 或 {{jsxref("Symbol.keyFor()")}} 要求特定類型,即必須提供。

+ +

實例

+ +

無效的情況下

+ +
// undefined 和 null 的情況下在其上的子方法不起作用
+var foo = undefined;
+foo.substring(1); // TypeError: foo is undefined
+
+var foo = null;
+foo.substring(1); // TypeError: foo is null
+
+
+// 某些方法可能要求特定類型
+var foo = {}
+Symbol.keyFor(foo); // TypeError: foo is not a symbol
+
+var foo = "bar"
+Object.create(foo); // TypeError: "foo" is not an object or null
+
+ +

修復問題

+ +

為了解決空指針 undefinednull 值,可以使用 typeof 運算符,例如。 operator, for example.

+ +
if (typeof foo !== 'undefined') {
+  // 現在我們知道foo被定義,我們可以繼續進行。
+}
+ +

參見

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