diff options
Diffstat (limited to 'files/de/web/javascript/reference/global_objects/function/tostring')
-rw-r--r-- | files/de/web/javascript/reference/global_objects/function/tostring/index.html | 236 |
1 files changed, 236 insertions, 0 deletions
diff --git a/files/de/web/javascript/reference/global_objects/function/tostring/index.html b/files/de/web/javascript/reference/global_objects/function/tostring/index.html new file mode 100644 index 0000000000..5b3978809e --- /dev/null +++ b/files/de/web/javascript/reference/global_objects/function/tostring/index.html @@ -0,0 +1,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> |