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/web/api/console/assert/index.html | 118 ++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 files/zh-tw/web/api/console/assert/index.html (limited to 'files/zh-tw/web/api/console/assert') diff --git a/files/zh-tw/web/api/console/assert/index.html b/files/zh-tw/web/api/console/assert/index.html new file mode 100644 index 0000000000..415b4cfa9a --- /dev/null +++ b/files/zh-tw/web/api/console/assert/index.html @@ -0,0 +1,118 @@ +--- +title: Console.assert() +slug: Web/API/console/assert +tags: + - API + - DOM + - Debugging + - 函式 + - 控制台 + - 網頁控制台 + - 網頁開發 +translation_of: Web/API/console/assert +--- +
{{APIRef("Console API")}}
+ +

如果斷言(assertion)為非(false),主控台會顯示錯誤訊息;如果斷言為是(true),則不發生任何事。

+ +

{{AvailableInWorkers}}

+ +
+

注意在 Node.js 內 console.assert() 方法的實做,與瀏覽器並不相同。

+ +

瀏覽器內呼叫 falsy 的 console.assert() 斷言出現 message,但不會中斷程式碼的執行。然而在 Node.js 裡面,falsy 斷言會拋出 AssertionError 錯誤。

+
+ +

語法

+ +
console.assert(assertion, obj1 [, obj2, ..., objN]);
+console.assert(assertion, msg [, subst1, ..., substN]); // c-like message formatting
+
+ +

參數

+ +
+
assertion
+
布林表達式。如果斷言為非,訊息會出現在主控台上。
+
obj1 ... objN
+
要印出來的 JavaScript 物件名單。 The string representations of each of these objects are appended together in the order listed and output.
+
msg
+
包含零個以上的 JavaScript 替代(substitution)字串。
+
subst1 ... substN
+
JavaScript objects with which to replace substitution strings within msg. This parameter gives you additional control over the format of the output.
+
+ +

請參見 {{domxref("console")}} 的 Outputting text to the console 以獲取詳細資訊。

+ +

範例

+ +

以下程式碼示範一個 JavaScript 物件的斷言使用:

+ +
const errorMsg = 'the # is not even';
+for (let number = 2; number <= 5; number += 1) {
+    console.log('the # is ' + number);
+    console.assert(number % 2 === 0, {number: number, errorMsg: errorMsg});
+    // or, using ES2015 object property shorthand:
+    // console.assert(number % 2 === 0, {number, errorMsg});
+}
+// output:
+// the # is 2
+// the # is 3
+// Assertion failed: {number: 3, errorMsg: "the # is not even"}
+// the # is 4
+// the # is 5
+// Assertion failed: {number: 5, errorMsg: "the # is not even"}
+
+ +

請注意,雖然包含替換字符串的字符串在 Node 中用作 console.log 的參數,但很多(如果不是大多數)瀏覽器...

+ +
console.log('the word is %s', 'foo');
+// output: the word is foo
+
+ +

...在所有瀏覽器中,使用此類字符串目前無法作為console.assert的參數使用:

+ +
console.assert(false, 'the word is %s', 'foo');
+// correct output in Node (e.g. v8.10.0) and some browsers
+//     (e.g. Firefox v60.0.2):
+// Assertion failed: the word is foo
+// incorrect output in some browsers
+//     (e.g. Chrome v67.0.3396.87):
+// Assertion failed: the word is %s foo
+
+ +

有關詳細信息,請參閱 {{domxref("console")}} 文檔中的將文本輸出到控制台

+ +

規範

+ + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName("Console API", "#assert", "console.assert()")}}{{Spec2("Console API")}}初始定義
+ +

瀏覽器相容性

+ + + +

{{Compat("api.Console.assert")}}

+ +

參見

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