aboutsummaryrefslogtreecommitdiff
path: root/files/de/web/javascript/reference/global_objects/error/tostring/index.html
blob: 15565bd296ae338d124eb22474f3f4a43578bce3 (plain)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
---
title: Error.prototype.toString()
slug: Web/JavaScript/Reference/Global_Objects/Error/toString
tags:
  - Error
  - JavaScript
  - Method
  - Prototype
translation_of: Web/JavaScript/Reference/Global_Objects/Error/toString
---
<div>{{JSRef}}</div>

<div>Die <code><strong>toString()</strong></code> Methode gibt einen String zurück, der das {{jsxref("Error")}} Objekt repräsentiert.</div>

<div> </div>

<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox"><code><var>e</var>.toString()</code></pre>

<h3 id="Rückgabewert">Rückgabewert</h3>

<p>Einen String, der das gegebenen {{jsxref("Error")}} Objekt repräsentiert.</p>

<h2 id="Beschreibung">Beschreibung</h2>

<p>Das {{jsxref("Error")}} Objekt überschreibt die {{jsxref("Object.prototype.toString()")}} Methode, die an allen Objekte vererbt werden. Die Semanik ist die folgende (angenommen {{jsxref("Object")}} und {{jsxref("String")}} wurden nicht verändert):</p>

<pre class="brush: js">Error.prototype.toString = function() {
  'use strict';

  var obj = Object(this);
  if (obj !== this) {
    throw new TypeError();
  }

  var name = this.name;
  name = (name === undefined) ? 'Error' : String(name);

  var msg = this.message;
  msg = (msg === undefined) ? '' : String(msg);

  if (name === '') {
    return msg;
  }
  if (msg === '') {
    return name;
  }

  return name + ': ' + msg;
};
</pre>

<h2 id="Beispiele">Beispiele</h2>

<pre class="brush: js">var e = new Error('fatal error');
console.log(e.toString()); // 'Error: fatal error'

e.name = undefined;
console.log(e.toString()); // 'Error: fatal error'

e.name = '';
console.log(e.toString()); // 'fatal error'

e.message = undefined;
console.log(e.toString()); // 'Error'

e.name = 'hello';
console.log(e.toString()); // 'hello'
</pre>

<h2 id="Spezifikationen">Spezifikationen</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Spezifikation</th>
   <th scope="col">Status</th>
   <th scope="col">Kommentar</th>
  </tr>
  <tr>
   <td>{{SpecName('ES1')}}</td>
   <td>{{Spec2('ES1')}}</td>
   <td>Initiale Definition. Implementiert in JavaScript 1.1.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.11.4.4', 'Error.prototype.toString')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-error.prototype.tostring', 'Error.prototype.toString')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-error.prototype.tostring', 'Error.prototype.toString')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="Browserkompatibilität">Browserkompatibilität</h2>

<div>


<p>{{Compat("javascript.builtins.Error.toString")}}</p>
</div>

<h2 id="Siehe_auch">Siehe auch</h2>

<ul>
 <li>{{jsxref("Error.prototype.toSource()")}}</li>
</ul>