aboutsummaryrefslogtreecommitdiff
path: root/files/zh-tw/web/api/console/assert/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'files/zh-tw/web/api/console/assert/index.html')
-rw-r--r--files/zh-tw/web/api/console/assert/index.html118
1 files changed, 118 insertions, 0 deletions
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
+---
+<div>{{APIRef("Console API")}}</div>
+
+<p>如果斷言(assertion)為非(false),主控台會顯示錯誤訊息;如果斷言為是(true),則不發生任何事。</p>
+
+<p>{{AvailableInWorkers}}</p>
+
+<div class="note">
+<p><strong>注意</strong>:<em>在 Node.js 內 <code>console.assert()</code> 方法的實做,與瀏覽器並不相同。</em></p>
+
+<p>瀏覽器內呼叫 falsy 的 <code>console.assert()</code> 斷言出現 <code>message</code>,但不會中斷程式碼的執行。然而在 Node.js 裡面,falsy 斷言會拋出 <code>AssertionError</code> 錯誤。</p>
+</div>
+
+<h2 id="語法">語法</h2>
+
+<pre class="syntaxbox">console.assert(<em>assertion</em>, <em>obj1</em> [, <em>obj2</em>, ..., <em>objN</em>]);
+console.assert(<em>assertion</em>, <em>msg</em> [, <em>subst1</em>, ..., <em>substN</em>]); // c-like message formatting
+</pre>
+
+<h3 id="參數">參數</h3>
+
+<dl>
+ <dt><code>assertion</code></dt>
+ <dd>布林表達式。如果斷言為非,訊息會出現在主控台上。</dd>
+ <dt><code>obj1</code> ... <code>objN</code></dt>
+ <dd>要印出來的 JavaScript 物件名單。 The string representations of each of these objects are appended together in the order listed and output.</dd>
+ <dt><code>msg</code></dt>
+ <dd>包含零個以上的 JavaScript 替代(substitution)字串。</dd>
+ <dt><code>subst1</code> ... <code>substN</code></dt>
+ <dd>JavaScript objects with which to replace substitution strings within <code>msg</code>. This parameter gives you additional control over the format of the output.</dd>
+</dl>
+
+<p>請參見 {{domxref("console")}} 的 <a href="/zh-TW/docs/Web/API/console#Outputting_text_to_the_console">Outputting text to the console</a> 以獲取詳細資訊。</p>
+
+<h2 id="範例">範例</h2>
+
+<p>以下程式碼示範一個 JavaScript 物件的斷言使用:</p>
+
+<pre class="brush: js">const errorMsg = 'the # is not even';
+for (let number = 2; number &lt;= 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"}
+</pre>
+
+<p>請注意,雖然包含替換字符串的字符串在 Node 中用作 <code>console.log</code> 的參數,但很多(如果不是大多數)瀏覽器...</p>
+
+<pre class="brush: js">console.log('the word is %s', 'foo');
+// output: the word is foo
+</pre>
+
+<p>...在所有瀏覽器中,使用此類字符串目前無法作為console.assert的參數使用:</p>
+
+<pre class="brush: js">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
+</pre>
+
+<p>有關詳細信息,請參閱 {{domxref("console")}} 文檔中的<a href="https://developer.mozilla.org/zh-TW/docs/Web/API/console#Outputting_text_to_the_console">將文本輸出到控制台</a>。</p>
+
+<h2 id="規範">規範</h2>
+
+<table class="standard-table">
+ <thead>
+ <tr>
+ <th scope="col">Specification</th>
+ <th scope="col">Status</th>
+ <th scope="col">Comment</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td>{{SpecName("Console API", "#assert", "console.assert()")}}</td>
+ <td>{{Spec2("Console API")}}</td>
+ <td>初始定義</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="瀏覽器相容性">瀏覽器相容性</h2>
+
+
+
+<p>{{Compat("api.Console.assert")}}</p>
+
+<h2 id="參見">參見</h2>
+
+<ul>
+ <li><a href="https://console.spec.whatwg.org/#assert-condition-data">WHATWG Console Standard: console.assert</a></li>
+ <li><a href="http://www.opera.com/dragonfly/documentation/console/">Opera Dragonfly documentation: Console</a></li>
+ <li><a href="http://msdn.microsoft.com/library/gg589530">MSDN: Using the F12 Tools Console to View Errors and Status</a></li>
+ <li><a href="https://developer.chrome.com/devtools/docs/console#assertions">Chrome Developer Tools: Using the Console</a></li>
+</ul>