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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
---
title: Function.prototype.toString()
slug: Web/JavaScript/Reference/Global_Objects/Function/toString
tags:
- Function
- JavaScript
- Method
- Prototype
translation_of: Web/JavaScript/Reference/Global_Objects/Function/toString
---
<div>{{JSRef}}</div>
<p>Die <code><strong>toString()</strong></code> Methode gibt eine Stringrepräsentation des Quelltextes einer Funktion zurück.</p>
<div>{{EmbedInteractiveExample("pages/js/function-tostring.html")}}</div>
<h2 id="Syntax">Syntax</h2>
<pre class="syntaxbox"><code><var>function</var>.toString()</code></pre>
<h3 id="Rückgabewert">Rückgabewert</h3>
<p>Eine Stringrepräsentation des Quelltextes der Funktion.</p>
<h2 id="Beschreibung">Beschreibung</h2>
<p>Das {{jsxref("Function")}} Objekt überschreibt die von {{jsxref("Object")}} geerbte Methode {{jsxref("Object.prototype.toString", "toString")}}; sie erbt nicht von {{jsxref("Object.prototype.toString")}}. Für benutzerdefinierte {{jsxref("Function")}} Objekte gibt die <code>toString</code> Methode einen String zurück, welcher den Quelltext, der die Funktion definiert, enthält.</p>
<p>JavaScript ruft die <code>toString</code> Methode automatisch auf, wenn einen {{jsxref("Function")}} als Text repräsentiert werden muss, z. B. wenn eine Funktion mit einem String konkateniert wird.</p>
<p>Die <code>toString()</code> Methode erzeugt eine {{jsxref("TypeError")}} Fehler("Function.prototype.toString called on incompatible object"), wenn das Objekt von <code>this</code> Objekt kein <code>Function</code> Objekt ist. Dieser wird auch bei einem {{jsxref("Proxy")}} Objekte erzeugt, zum Beispiel:</p>
<pre class="brush: js example-bad">Function.prototype.toString.call('foo'); // TypeError
var proxy = new Proxy(function() {}, {});
Function.prototype.toString.call(proxy); // TypeError
</pre>
<p>Wenn die <code>toString()</code> Methode auf eingebauten Objekten oder einer von <code>Function.prototype.bind</code> erstellten Methode aufgerufen wird, gibt <code>toString() </code><em>native function string</em> zurück, was wie folgt aussiet:</p>
<pre class="brush: js">"function () {\n [native code]\n}"
</pre>
<p>Wenn die <code>toString()</code> Methode auf einer Funktion aufgerufen wird, die mit dem <code>Function</code> Konstruktor erstellt wurde, gibt diese den Quelltext der syntetischen Funktionsdeklerations mit dem Namen "anonymous" zurück, welche die Parameter und den Funktionrumpf enthält.</p>
<h2 id="Beispiele">Beispiele</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Funktion</th>
<th scope="col">Ergebnis von Function.prototype.toString</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<pre>
function f(){}</pre>
</td>
<td>
<pre>
"function f(){}"</pre>
</td>
</tr>
<tr>
<td>
<pre>
class A { a(){} }</pre>
</td>
<td>
<pre>
"class A { a(){} }"</pre>
</td>
</tr>
<tr>
<td>
<pre>
function* g(){}</pre>
</td>
<td>
<pre>
"function* g(){}"</pre>
</td>
</tr>
<tr>
<td>
<pre>
a => a</pre>
</td>
<td>
<pre>
"a => a"</pre>
</td>
</tr>
<tr>
<td>
<pre>
({ a(){} }.a)</pre>
</td>
<td>
<pre>
"a(){}"</pre>
</td>
</tr>
<tr>
<td>
<pre>
({ *a(){} }.a)</pre>
</td>
<td>
<pre>
"*a(){}"</pre>
</td>
</tr>
<tr>
<td>
<pre>
({ [0](){} }[0])</pre>
</td>
<td>
<pre>
"[0](){}"</pre>
</td>
</tr>
<tr>
<td>
<pre>
Object.getOwnPropertyDescriptor({
get a(){}
}, "a").get</pre>
</td>
<td>
<pre>
"get a(){}"</pre>
</td>
</tr>
<tr>
<td>
<pre>
Object.getOwnPropertyDescriptor({
set a(x){}
}, "a").set</pre>
</td>
<td>
<pre>
"set a(x){}"</pre>
</td>
</tr>
<tr>
<td>
<pre>
Function.prototype.toString</pre>
</td>
<td>
<pre>
"function toString() { [native code] }"</pre>
</td>
</tr>
<tr>
<td>
<pre>
(function f(){}.bind(0))</pre>
</td>
<td>
<pre>
"function () { [native code] }"</pre>
</td>
</tr>
<tr>
<td>
<pre>
Function("a", "b")</pre>
</td>
<td>
<pre>
"function anonymous(a\n) {\nb\n}"</pre>
</td>
</tr>
</tbody>
</table>
<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('ES6', '#sec-function.prototype.tostring', 'Function.prototype.toString')}}</td>
<td>{{Spec2('ES6')}}</td>
<td>Spezifischere Anforderungen wurden an die Stringrepräsentation hinzugefügt.</td>
</tr>
<tr>
<td><a href="http://tc39.github.io/Function-prototype-toString-revision/">Function.prototype.toString revision</a></td>
<td>Draft</td>
<td>Standardisierung nativer Funktionsstrings am Zeilenende.</td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-function.prototype.tostring', 'Function.prototype.toString')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="Browserkompatibilität">Browserkompatibilität</h2>
<div>
<p>{{Compat("javascript.builtins.Function.toString")}}</p>
</div>
<h2 id="Firefox_spezifische_Hinweise">Firefox spezifische Hinweise</h2>
<ul>
<li>Seit Firefox 17, ist <code>Function.prototype.toString()</code> implementiert zum Speichern von Funktionsquelltexten. Der Decompiler wurde entfernt, so das der <code>indentation</code> Parameter nicht mehr gebraucht wird. Für mehr Details siehe {{bug("761723")}}.</li>
<li>Seit Firefox 38, erzeugt <code>Function.prototype.toString()</code> einen Fehler für {{jsxref("Proxy")}} Objekts ({{bug(1100936)}}).</li>
</ul>
<h2 id="Siehe_auch">Siehe auch</h2>
<ul>
<li>{{jsxref("Object.prototype.toString()")}}</li>
</ul>
|