1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
---
title: Console.assert()
slug: Web/API/Console/assert
translation_of: Web/API/console/assert
---
<div>{{APIRef("Console API")}}</div>
<p>Schreibt eine Fehlermeldung in die Konsole, wenn eine Behauptung falsch ist. Wenn die Behauptung wahr ist, passiert nichts.</p>
<p>{{AvailableInWorkers}}</p>
<p>Die Methode <code>console.assert()</code> wurde in älteren Node.js Versionen anders implementiert als in Browsern. In Browsern wird durch den Aufruf von <code>console.assert()</code> mit einer falschen Behauptung <code>message</code> in die Konsole ausgegeben, <em>ohne</em> die Ausführung des nachfolgenden Codes zu unterbrechen. Vor Node.js v10.0.0 bewirkt eine falsche Behauptung jedoch auch, dass ein <code>AssertionError</code> ausgelöst wird. Diese Diskrepanz wurde mit Node v10 behoben, so dass <code>console.assert()</code> jetzt sowohl in Node als auch im Browser gleich funktioniert.</p>
<h2 id="Syntax">Syntax</h2>
<pre class="brush: js">console.assert(<var>assertion</var>, <var>obj1</var> [, <var>obj2</var>, ..., <var>objN</var>]);
console.assert(<var>assertion</var>, <var>msg</var> [, <var>subst1</var>, ..., <var>substN</var>]); // c-ähnliche Formatierung
</pre>
<h3 id="Parameter">Parameter</h3>
<dl>
<dt><code>assertion</code></dt>
<dd>Jeder boolesche Ausdruck. Wenn die Behauptung falsch ist, wird <code>message</code> in der Console ausgegeben.</dd>
<dt><code>obj1</code> ... <code>objN</code></dt>
<dd>Eine Liste der auszugebenden JavaScript-Objekte. Die String-Darstellungen jedes dieser Objekte werden in der angegebenen Reihenfolge zusammengefügt und ausgegeben.</dd>
<dt><code>msg</code></dt>
<dd>Ein JavaScript-String, der keine oder mehrere Ersetzungsstrings enthält.</dd>
<dt><code>subst1</code> ... <code>substN</code></dt>
<dd>JavaScript-Objekte, mit denen Ersetzungsstrings in msg ersetzt werden sollen. Dieser Parameter gibt Ihnen zusätzliche Kontrolle über das Format der Ausgabe.</dd>
</dl>
<h2 id="Beispiele">Beispiele</h2>
<p>Im folgenden Codebeispiel wird die Verwendung eines JavaScript-Objekts nach der Behauptung veranschaulicht:</p>
<pre class="brush: js">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});
// oder mit ES2015 Object Property Shorthand:
// console.assert(number % 2 === 0, {number, errorMsg});
}
// Ausgabe:
// 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>Beachten Sie, dass ein String, der einen Ersatzstring enthält, als Parameter für <code>console.log()</code> in Node und vielen, wenn nicht allen Browsern, funktioniert...</p>
<pre class="brush: js">console.log('the word is %s', 'foo');
// Ausgabe: the word is foo</pre>
<p>...die Verwendung eines solchen Strings als Parameter für <code>console.assert()</code> jedoch derzeit nicht in allen Browsern wie erwartet funktioniert:</p>
<pre class="brush: js">console.assert(false, 'the word is %s', 'foo');
// korrekte Ausgabe in Node (e.g. v8.10.0) und einigen Browsern
// (z. B. Firefox v60.0.2):
// Assertion failed: the word is foo
// inkorrekte Ausgabe in einigen Browsern
// (z. B. Chrome v67.0.3396.87):
// Assertion failed: the word is %s foo</pre>
<p>Weitere Informationen finden Sie in der Dokumentation von {{domxref("console")}} unter <a href="/de/docs/Web/API/console#Outputting_text_to_the_console">Text in der Konsole ausgeben</a>.</p>
<h2 id="Spezifikationen">Spezifikationen</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Spezifikation</th>
<th scope="col">Status</th>
<th scope="col">Kommentar</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName("Console API", "#assert", "console.assert()")}}</td>
<td>{{Spec2("Console API")}}</td>
<td>Initiale Definition</td>
</tr>
</tbody>
</table>
<h2 id="Browserkompatibilität">Browserkompatibilität</h2>
<p>{{Compat("api.Console.assert")}}</p>
<h2 id="Siehe_auch">Siehe auch</h2>
<ul>
<li><a href="https://console.spec.whatwg.org/#assert-condition-data">WHATWG Console Standard: console.assert</a></li>
<li><a href="https://docs.microsoft.com/de-de/microsoft-edge/devtools-guide/console/console-api">Console API - Microsoft Edge Development | Microsoft Docs</a></li>
<li><a href="https://developers.google.com/web/tools/chrome-devtools/console/api#assert">Chrome Developer Tools: Using the Console</a></li>
</ul>
|