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
|
---
title: Object.keys()
slug: Web/JavaScript/Reference/Global_Objects/Object/keys
tags:
- ECMAScript5
- JavaScript
- JavaScript 1.8.5
- Method
- Object
translation_of: Web/JavaScript/Reference/Global_Objects/Object/keys
---
<div>{{JSRef}}</div>
<p>Die <strong><code>Object.keys()</code></strong> Funktion gibt ein Array zurück, das die eigenen aufzählbaren Eigenschaften des Objektes in der selben Reihenfolge enthält wie in der {{jsxref("Statements/for...in", "for...in")}} Schleife (der Unterschied zwischen diesen beiden Varianten besteht darin, dass eine for-in Schleife auch die aufzählbaren Eigenschaften der Prototypen beinhaltet).</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox"><code>Object.keys(<var>obj</var>)</code></pre>
<h3 id="Parameters" name="Parameters">Parameter</h3>
<dl>
<dt><code>obj</code></dt>
<dd>ein Objekt, dessen aufzählbare eigene Eigenschaften zurückgegeben werden sollen</dd>
</dl>
<h3 id="Description" name="Description">Rückgabewert</h3>
<p>Ein Array mit Strings, die alle aufzählbare Eigenschaften des gegebenen Objektes repräsentieren.</p>
<h2 id="Description" name="Description">Beschreibung</h2>
<p><code>Object.keys()</code> liefert ein Array, dessen Elemente Strings sind, welche die aufzählbaren Eigenschaften des Objekts respräsentieren. Die Reihenfolge der Eigenschaften ist die selbe wie bei einer for-in Schleife über das Objekt.</p>
<h2 id="Beispiele">Beispiele</h2>
<pre class="brush: js">var arr = ['a', 'b', 'c'];
console.log(Object.keys(arr)); // console: ['0', '1', '2']
// array like object
var obj = { 0: 'a', 1: 'b', 2: 'c' };
console.log(Object.keys(obj)); // console: ['0', '1', '2']
// array like object with random key ordering
var an_obj = { 100: 'a', 2: 'b', 7: 'c' };
console.log(Object.keys(an_obj)); // console: ['2', '7', '100']
// getFoo is property which isn't enumerable
var my_obj = Object.create({}, { getFoo: { value: function() { return this.foo; } } });
my_obj.foo = 1;
console.log(Object.keys(my_obj)); // console: ['foo']</pre>
<p><span style="line-height: 1.5;">Die Liste aller eigenen aufzählbaren und nicht-aufzählbaren Eigenschaften eines Objekt erhält man mit der Funktion {{jsxref("Object.getOwnPropertyNames()")}}.</span></p>
<h2 id="Notes" name="Notes">Notizen</h2>
<p>In ES5 wird, wenn das Argument für die Funktion kein Objekt ist, ein {{jsxref("TypeError")}} Fehler geworfen. In ES2015 wird das Argument, welches kein Objekt ist, in ein Objekt umgewandelt, das aber nicht mehr dem eigentlichen Wert entspricht (siehe Beispiel).</p>
<pre class="brush: js">> Object.keys("foo")
TypeError: "foo" is not an object // ES5 code
> Object.keys("foo")
["0", "1", "2"] // ES2015 code
</pre>
<h2 id="Polyfill">Polyfill</h2>
<p>Kopieren Sie das folgende Code-Schnipsel, um <code>Object.keys()</code> auch in älteren Umgebungen ohne native Unterstützung nutzen zu können:</p>
<pre class="brush: js">// From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
if (!Object.keys) {
Object.keys = (function() {
'use strict';
var hasOwnProperty = Object.prototype.hasOwnProperty,
hasDontEnumBug = !({ toString: null }).propertyIsEnumerable('toString'),
dontEnums = [
'toString',
'toLocaleString',
'valueOf',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerable',
'constructor'
],
dontEnumsLength = dontEnums.length;
return function(obj) {
if (typeof obj !== 'function' && (typeof obj !== 'object' || obj === null)) {
throw new TypeError('Object.keys called on non-object');
}
var result = [], prop, i;
for (prop in obj) {
if (hasOwnProperty.call(obj, prop)) {
result.push(prop);
}
}
if (hasDontEnumBug) {
for (i = 0; i < dontEnumsLength; i++) {
if (hasOwnProperty.call(obj, dontEnums[i])) {
result.push(dontEnums[i]);
}
}
}
return result;
};
}());
}
</pre>
<p>Bitte beachten Sie, dass der obenstehende Code auch die Namen der nicht-aufzählbaren Eigenschaften im IE7 (und evtl. auch im IE8) berücksichtigt, wenn ein Objekt aus einem anderen Fenster übergeben wird.</p>
<p>Ein einfaches Polyfill finden Sie außerdem hier: <a href="http://tokenposts.blogspot.com.au/2012/04/javascript-objectkeys-browser.html">Javascript - Object.keys Browser Compatibility</a>.</p>
<h2 id="Spezifikationen">Spezifikationen</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Spezifikation</th>
<th scope="col">Status</th>
<th scope="col">Kommantar</th>
</tr>
<tr>
<td>{{SpecName('ES5.1', '#sec-15.2.3.14', 'Object.keys')}}</td>
<td>{{Spec2('ES5.1')}}</td>
<td>Initiale Definition. Implementiert in JavaScript 1.8.5.</td>
</tr>
<tr>
<td>{{SpecName('ES2015', '#sec-object.keys', 'Object.keys')}}</td>
<td>{{Spec2('ES2015')}}</td>
<td> </td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-object.keys', 'Object.keys')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="Browserkompatibilität">Browserkompatibilitä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>Grundlegende Unterstützung</td>
<td>{{CompatChrome("5")}}</td>
<td>{{CompatGeckoDesktop("2.0")}}</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>Grundlegende Unterstützung</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="See_also" name="See_also">Siehe auch</h2>
<ul>
<li><a href="/de/docs/Enumerability_and_ownership_of_properties">Enumerability and ownership of properties</a></li>
<li>{{jsxref("Object.prototype.propertyIsEnumerable()")}}</li>
<li>{{jsxref("Object.create()")}}</li>
<li>{{jsxref("Object.getOwnPropertyNames()")}}</li>
<li>{{jsxref("Object.values()")}} {{experimental_inline}}</li>
<li>{{jsxref("Object.entries()")}} {{experimental_inline}}</li>
</ul>
|