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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
---
title: Object.create()
slug: Web/JavaScript/Reference/Global_Objects/Object/create
tags:
- Methode(2)
- Méthode
- Objekt
- Referenz
translation_of: Web/JavaScript/Reference/Global_Objects/Object/create
---
<div>{{JSRef("Global_Objects", "Object")}}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p>Die <code><strong>Object.create()</strong></code> Methode erstellt ein neues Objekt mit dem angegebenen Prototype object und ggf. zusätzlichen Eigenschaften (properties).</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox"><code>Object.create(<var>proto</var>[, <var>propertiesObject</var>])</code></pre>
<h3 id="Parameters" name="Parameters">Parameter</h3>
<dl>
<dt><code>proto</code></dt>
<dd>Das Objekt, welches als Prototype object des zu erstellenden Objekts verwendet wird.</dd>
<dt><code>propertiesObject</code></dt>
<dd>Optional. Falls angegeben und nicht {{jsxref("Global_Objects/undefined", "undefined")}}, ein Objekt dessen aufzählbare eigene Eigenschaften (properties, d.h.: Eigenschaften, die für das Objekt selbst definiert wurden und nicht in der prototype chain) als Eigenschaften dem neu erstellten Objekt hinzugefügt werden. Die Eigenschaften entsprechen dem zweiten Parameter von {{jsxref("Object.defineProperties()")}}.</dd>
</dl>
<h3 id="Rückgabewert">Rückgabewert</h3>
<p>Ein neu erzeugtes Objekt mit dem angegebenen Prototype object und den angebenenen Eigenschaften (properties).</p>
<h3 id="Throws" name="Throws">Ausnahmen</h3>
<p>Eine {{jsxref("Global_Objects/TypeError", "TypeError")}}-Ausnahme wird ausgelöst, wenn der <code>proto</code>-Parameter weder {{jsxref("Global_Objects/null", "null")}} noch ein Objekt ist.</p>
<h2 id="Examples" name="Examples">Beispiele</h2>
<h3 id="Example:_Classical_inheritance_with_Object.create" name="Example:_Classical_inheritance_with_Object.create">Beispiel: Klassenbasierte Vererbung mit <code>Object.create()</code></h3>
<p>Im folgenden sehen Sie ein Beispiel wie man <code>Object.create()</code> verwenden kann, um klassenbasierte Vererbung zu realisieren. JavaScript unterstützt nur einzelne Vererbung.</p>
<pre class="brush: js">// Shape - superclass
function Shape() {
this.x = 0;
this.y = 0;
}
// superclass method
Shape.prototype.move = function(x, y) {
this.x += x;
this.y += y;
console.info('Shape moved.');
};
// Rectangle - subclass
function Rectangle() {
Shape.call(this); // call super constructor.
}
// subclass extends superclass
Rectangle.prototype = Object.create(Shape.prototype);
Rectangle.prototype.constructor = Rectangle;
var rect = new Rectangle();
console.log('Is rect an instance of Rectangle? ' + (rect instanceof Rectangle)); // true
console.log('Is rect an instance of Shape? ' + (rect instanceof Shape)); // true
rect.move(1, 1); // Outputs, 'Shape moved.'
</pre>
<p>Wenn Sie von mehreren Objekten erben wollen, können mixins verwendet werden.</p>
<pre class="brush: js">function MyClass() {
SuperClass.call(this);
OtherSuperClass.call(this);
}
MyClass.prototype = Object.create(SuperClass.prototype); // inherit
mixin(MyClass.prototype, OtherSuperClass.prototype); // mixin
MyClass.prototype.myMethod = function() {
// do a thing
};
</pre>
<p>Die <code>mixin</code> Funktion kopiert die Funktionen des superclass Prototypen in den subclass Prototyp. Die mixin Funktion muss vom Benutzer bereitgestellt werden. Ein Beispiel für eine mixin-ähnliche Funktion ist <a href="http://api.jquery.com/jQuery.extend/">jQuery.extend()</a>.</p>
<h3 id="Example:_Using_propertiesObject_argument_with_Object.create" name="Example:_Using_propertiesObject_argument_with_Object.create">Beispiel: Benutzung des <code>propertiesObject</code> Parameters von <code>Object.create()</code></h3>
<pre class="brush: js">var o;
// create an object with null as prototype
o = Object.create(null);
o = {};
// is equivalent to:
o = Object.create(Object.prototype);
// Example where we create an object with a couple of sample properties.
// (Note that the second parameter maps keys to *property descriptors*.)
o = Object.create(Object.prototype, {
// foo is a regular 'value property'
foo: { writable: true, configurable: true, value: 'hello' },
// bar is a getter-and-setter (accessor) property
bar: {
configurable: false,
get: function() { return 10; },
set: function(value) { console.log('Setting `o.bar` to', value); }
/* with ES5 Accessors our code can look like this
get function() { return 10; },
set function(value) { console.log('setting `o.bar` to', value); } */
}
});
function Constructor() {}
o = new Constructor();
// is equivalent to:
o = Object.create(Constructor.prototype);
// Of course, if there is actual initialization code in the
// Constructor function, the Object.create() cannot reflect it
// create a new object whose prototype is a new, empty object
// and a adding single property 'p', with value 42
o = Object.create({}, { p: { value: 42 } });
// by default properties ARE NOT writable, enumerable or configurable:
o.p = 24;
o.p;
// 42
o.q = 12;
for (var prop in o) {
console.log(prop);
}
// 'q'
delete o.p;
// false
// to specify an ES3 property
o2 = Object.create({}, {
p: {
value: 42,
writable: true,
enumerable: true,
configurable: true
}
});
</pre>
<h2 id="Polyfill" name="Polyfill">Polyfill</h2>
<p>Dieser Polyfill deckt den Hauptanwendungsfall, die Erzeugung eines neuen Objektes für das ein Prototyp ausgewählt wurde, ab.</p>
<p>Bitte beachten Sie, dass dieses Polyfill im Gegensatz zum echten ES5 <code>Object.create</code> den Einsatz von <code>null</code> als Prototyp-Parameter aufgrund einer Einschränkung von ECMAScript vor Version 5 nicht unterstützt.</p>
<pre class="brush: js line-numbers language-js"><code class="language-js"><span class="keyword token">if</span> <span class="punctuation token">(</span><span class="keyword token">typeof</span> Object<span class="punctuation token">.</span>create <span class="operator token">!=</span> <span class="string token">'function'</span><span class="punctuation token">)</span> <span class="punctuation token">{</span>
Object<span class="punctuation token">.</span>create <span class="operator token">=</span> <span class="punctuation token">(</span><span class="keyword token">function</span><span class="punctuation token">(</span>undefined<span class="punctuation token">)</span> <span class="punctuation token">{</span>
<span class="keyword token">var</span> Temp <span class="operator token">=</span> <span class="keyword token">function</span><span class="punctuation token">(</span><span class="punctuation token">)</span> <span class="punctuation token">{</span><span class="punctuation token">}</span><span class="punctuation token">;</span>
<span class="keyword token">return</span> <span class="keyword token">function</span> <span class="punctuation token">(</span>prototype<span class="punctuation token">,</span> propertiesObject<span class="punctuation token">)</span> <span class="punctuation token">{</span>
<span class="keyword token">if</span><span class="punctuation token">(</span>prototype <span class="operator token">!==</span> <span class="function token">Object</span><span class="punctuation token">(</span>prototype<span class="punctuation token">)</span> <span class="operator token">&&</span> prototype <span class="operator token">!==</span> <span class="keyword token">null</span><span class="punctuation token">)</span> <span class="punctuation token">{</span>
<span class="keyword token">throw</span> <span class="function token">TypeError</span><span class="punctuation token">(</span><span class="string token">'Argument must be an object, or null'</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
<span class="punctuation token">}</span>
Temp<span class="punctuation token">.</span>prototype <span class="operator token">=</span> prototype <span class="operator token">||</span> <span class="punctuation token">{</span><span class="punctuation token">}</span><span class="punctuation token">;</span>
<span class="keyword token">if</span> <span class="punctuation token">(</span>propertiesObject <span class="operator token">!==</span> undefined<span class="punctuation token">)</span> <span class="punctuation token">{</span>
Object<span class="punctuation token">.</span><span class="function token">defineProperties</span><span class="punctuation token">(</span>Temp<span class="punctuation token">.</span>prototype<span class="punctuation token">,</span> propertiesObject<span class="punctuation token">)</span><span class="punctuation token">;</span>
<span class="punctuation token">}</span>
<span class="keyword token">var</span> result <span class="operator token">=</span> <span class="keyword token">new</span> <span class="class-name token">Temp</span><span class="punctuation token">(</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
Temp<span class="punctuation token">.</span>prototype <span class="operator token">=</span> <span class="keyword token">null</span><span class="punctuation token">;</span>
<span class="comment token">// to imitate the case of Object.create(null)</span>
<span class="keyword token">if</span><span class="punctuation token">(</span>prototype <span class="operator token">===</span> <span class="keyword token">null</span><span class="punctuation token">)</span> <span class="punctuation token">{</span>
result<span class="punctuation token">.</span>__proto__ <span class="operator token">=</span> <span class="keyword token">null</span><span class="punctuation token">;</span>
<span class="punctuation token">}</span>
<span class="keyword token">return</span> result<span class="punctuation token">;</span>
<span class="punctuation token">}</span><span class="punctuation token">;</span>
<span class="punctuation token">}</span><span class="punctuation token">)</span><span class="punctuation token">(</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
<span class="punctuation token">}</span></code></pre>
<h2 id="Specifications" name="Specifications">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.5', 'Object.create')}}</td>
<td>{{Spec2('ES5.1')}}</td>
<td>Initial definition. Implemented in JavaScript 1.8.5.</td>
</tr>
<tr>
<td>{{SpecName('ES6', '#sec-object.create', 'Object.create')}}</td>
<td>{{Spec2('ES6')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility" name="Browser_compatibility">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>Basic support</td>
<td>{{CompatChrome("5")}}</td>
<td>{{CompatGeckoDesktop("2")}}</td>
<td>{{CompatIE("9")}}</td>
<td>{{CompatOpera("11.60")}}</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>Basic support</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatGeckoDesktop("2")}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatOperaMobile("11.50")}}</td>
<td>{{CompatVersionUnknown}}</td>
</tr>
</tbody>
</table>
</div>
<p>Basierend auf <a href="http://kangax.github.com/es5-compat-table/">Kangax's compat table</a>.</p>
<h2 id="See_also" name="See_also">Zusätzliches Material</h2>
<ul>
<li>{{jsxref("Object.defineProperty()")}}</li>
<li>{{jsxref("Object.defineProperties()")}}</li>
<li>{{jsxref("Object.prototype.isPrototypeOf()")}}</li>
<li>John Resig's post on <a href="http://ejohn.org/blog/objectgetprototypeof/">getPrototypeOf()</a></li>
</ul>
|