diff options
Diffstat (limited to 'files/de/web/javascript/reference/global_objects/object/getownpropertynames/index.html')
| -rw-r--r-- | files/de/web/javascript/reference/global_objects/object/getownpropertynames/index.html | 201 |
1 files changed, 201 insertions, 0 deletions
diff --git a/files/de/web/javascript/reference/global_objects/object/getownpropertynames/index.html b/files/de/web/javascript/reference/global_objects/object/getownpropertynames/index.html new file mode 100644 index 0000000000..1e76977b49 --- /dev/null +++ b/files/de/web/javascript/reference/global_objects/object/getownpropertynames/index.html @@ -0,0 +1,201 @@ +--- +title: Object.getOwnPropertyNames() +slug: Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames +tags: + - german +translation_of: Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames +--- +<div>{{JSRef}}</div> + +<p>Die <strong><code>Object.getOwnPropertyNames()</code></strong> Methode gibt einen Array mit allen Eigenschaftsnamen (aufzählbar oder nicht) zurück, welche direkt auf einem Objekt gefunden werden. </p> + +<h2 id="Syntax">Syntax</h2> + +<pre class="syntaxbox"><code>Object.getOwnPropertyNames(<var>obj</var>)</code></pre> + +<h3 id="Parameters">Parameters</h3> + +<dl> + <dt><code>obj</code></dt> + <dd>Das Objekt dessen aufzählbaren und nicht aufzählbaren Eigenschaftsnamen gesucht sind. </dd> +</dl> + +<h2 id="Beschreibung">Beschreibung</h2> + +<p>Object.getOwnPropertyNames () gibt ein Array zurück, dessen Elemente Strings sind, die den aufzählbar und nicht aufzählbar Eigenschaften direkt auf dem Objekt gefunden werden. Die Reihenfolge der Enumerable-Objekte im Array steht im Einklang mit der Benutzung von einer exponierten Schleife {{jsxref ("Statements / for ... in", "for ... in")}} (oder durch {{jsxref ("Object.keys () ")}}) über die Eigenschaften des Objekts. Die Reihenfolge der nicht-aufzählbar Objekte im Array, und unter den aufzählbare Eigenschaften, ist nicht definiert.</p> + +<h2 id="Beispiele">Beispiele</h2> + +<h3 id="Benutzung_von_Object.getOwnPropertyNames()"><code>Benutzung von Object.getOwnPropertyNames()</code></h3> + +<pre class="brush: js">var arr = ['a', 'b', 'c']; +console.log(Object.getOwnPropertyNames(arr).sort()); // logs '0,1,2,length' + +// Array-like object +var obj = { 0: 'a', 1: 'b', 2: 'c' }; +console.log(Object.getOwnPropertyNames(obj).sort()); // logs '0,1,2' + +// Logging property names and values using Array.forEach +Object.getOwnPropertyNames(obj).forEach(function(val, idx, array) { + console.log(val + ' -> ' + obj[val]); +}); +// logs +// 0 -> a +// 1 -> b +// 2 -> c + +// non-enumerable property +var my_obj = Object.create({}, { + getFoo: { + value: function() { return this.foo; }, + enumerable: false + } +}); +my_obj.foo = 1; + +console.log(Object.getOwnPropertyNames(my_obj).sort()); // logs 'foo,getFoo' +</pre> + +<p>Wenn Sie nur die aufzählbare Eigenschaften möchten, kann man die Funktion {{jsxref ("Object.keys ()")}} benutzen. Alternativ können auch Schlaufen {{jsxref ("Statements / for ... in", "for ... in")}} verwendet werden (dies gibt nicht nur die aufzählbaren Eigenschaften des Objektes, sondern auch entlang der Prototypkette zurück. Behoben kann diese mit der Methode {{jsxref ("Object.prototype.hasOwnProperty ()", "hasOwnProperty ()" )}}).</p> + +<p>Items welche sich im Prototype chain befinden, werden nicht aufgelistet.</p> + +<pre class="brush: js">function ParentClass() {} +ParentClass.prototype.inheritedMethod = function() {}; + +function ChildClass() { + this.prop = 5; + this.method = function() {}; +} +ChildClass.prototype = new ParentClass; +ChildClass.prototype.prototypeMethod = function() {}; + +console.log( + Object.getOwnPropertyNames( + new ChildClass() // ['prop', 'method'] + ) +); +</pre> + +<h3 id="Nur_unzählige_Eigenschaften">Nur unzählige Eigenschaften</h3> + +<p>Es wird die {{jsxref("Array.prototype.filter()")}} Funktion benötigt um alle zählbaren Schlüssel (erhalten mit {{jsxref("Object.keys()")}}) von einer Liste mit allen Schlüsseln (erhalten mit <code>Object.getOwnPropertyNames()</code>) zu vergleichen, welches nur die unzähligen Eigenschaften zurück lässt.</p> + +<pre class="brush: js">var target = myObject; +var enum_and_nonenum = Object.getOwnPropertyNames(target); +var enum_only = Object.keys(target); +var nonenum_only = enum_and_nonenum.filter(function(key) { + var indexInEnum = enum_only.indexOf(key); + if (indexInEnum == -1) { + // not found in enum_only keys mean the key is non-enumerable, + // so return true so we keep this in the filter + return true; + } else { + return false; + } +}); + +console.log(nonenum_only); +</pre> + +<h2 id="Notizen">Notizen</h2> + +<p>Wenn bei ES5 das Argument der Methode kein primitives Objekt ist, wird der Funktionsaufruf ein {{jsxref("TypeError")}} werfen. In ES6 wird ein nicht-Objekt Argument automatisch in ein Objekt gecasted.</p> + +<pre class="brush: js">Object.getOwnPropertyNames('foo'); +// TypeError: "foo" is not an object (ES5 code) + +Object.getOwnPropertyNames('foo'); +// ['length', '0', '1', '2'] (ES6 code) +</pre> + +<h2 id="Spezifikationen">Spezifikationen</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.2.3.4', 'Object.getOwnPropertyNames')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td>Initialdefinition. Implementiert in JavaScript 1.8.5.</td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-object.getownpropertynames', 'Object.getOwnPropertyNames')}}</td> + <td>{{Spec2('ES6')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="Browser_Kompatibilität">Browser Kompatibilität</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Basissupport</td> + <td>{{CompatChrome("5")}}</td> + <td>{{CompatGeckoDesktop("2")}}</td> + <td>{{CompatIE("9")}}</td> + <td>{{CompatOpera("12")}}</td> + <td>{{CompatSafari("5")}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Chrome for Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basissupport</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="SpiderMonkey-spezifische_Notizen">SpiderMonkey-spezifische Notizen</h2> + +<p> </p> + +<p>Vor SpiderMonkey 28 {{geckoRelease("28")}}, ignorierte die Funktion <code>Object.getOwnPropertyNames</code> unaufgelöste Eigenschaften eines {{jsxref("Error")}} Objektes. Dieser Fehler wurde in den letzten Versionen behoben ({{bug("724768")}}).</p> + +<h2 id="Siehe_auch">Siehe auch</h2> + +<ul> + <li><a href="/en-US/docs/Enumerability_and_ownership_of_properties">Enumerability and ownership of properties</a></li> + <li>{{jsxref("Object.prototype.hasOwnProperty()")}}</li> + <li>{{jsxref("Object.prototype.propertyIsEnumerable()")}}</li> + <li>{{jsxref("Object.create()")}}</li> + <li>{{jsxref("Object.keys()")}}</li> + <li>{{jsxref("Array.forEach()")}}</li> +</ul> |
