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
|
---
title: Object.prototype.constructor
slug: Web/JavaScript/Reference/Global_Objects/Object/constructor
tags:
- JavaScript
- Object
- Property
- Prototype
translation_of: Web/JavaScript/Reference/Global_Objects/Object/constructor
---
<div>{{JSRef("Global_Objects", "Object")}}</div>
<h2 id="Summary" name="Summary">Zusammenfassung</h2>
<p>Gibt eine Referenz zur {{jsxref("Global_Objects/Object", "Object")}}-Funktion, welche die Instanz vom Prototyp herstellt. Merke: Der Wert dieser Eigenschaft ist eine Referenz auf die Funktion selbst und kein String mit dem Funktionsnamen als Inhalt. Der Wert ist nur lesbar für primitive Werte, wie <code>1</code>, <code>true</code> und <code>"test"</code>.</p>
<h2 id="Description" name="Description">Beschreibung</h2>
<p>Alle Objekte erben eine constructor- Eigenschaft von ihrem <code>prototype</code>:</p>
<pre class="brush: js">var o = {};
o.constructor === Object; // true
var a = [];
a.constructor === Array; // true
var n = new Number(3);
n.constructor === Number; // true
</pre>
<h2 id="Examples" name="Examples">Beispiele</h2>
<h3 id="Example:_Displaying_the_constructor_of_an_object" name="Example:_Displaying_the_constructor_of_an_object">Beispiel: zeigen den Konstruktor von einem Objekt an</h3>
<p>Das folgende Beispiel erzeugt einen Prototyp: <code>Tree</code>, und ein Objekt von jenem Typ: <code>theTree</code>. Das Beispiel zeigt danach die Konstruktor-Eigenschaft vom <code>Objekt:theTree</code> an.</p>
<pre class="brush: js">function Tree(name) {
this.name = name;
}
var theTree = new Tree('Redwood');
console.log('theTree.constructor is ' + theTree.constructor);
</pre>
<p>Das Beispiel erzeugt folgende Ausgabe:</p>
<pre class="brush: js">theTree.constructor is function Tree(name) {
this.name = name;
}
</pre>
<h3 id="Example:_Changing_the_constructor_of_an_object" name="Example:_Changing_the_constructor_of_an_object">Beispiel: Veränderung des Konstruktors eines Objekts</h3>
<p>Das folgende Beispiel zeigt, wie man den Konstruktorwert eines generischen Objekts verändert. Nur <code>true</code>, <code>1</code> und <code>"test"</code> werden nicht von der Veränderung betroffen, da sie nur lesbare (read-only) native Konstruktoren haben. Dieses Beispiel zeigt, dass es nicht immer sicher ist, sich auf die Konstruktor-Eigenschaft eines Objekts zu verlassen.</p>
<pre class="brush:js">function Type () {}
var types = [
new Array(),
[],
new Boolean(),
true, // bleibt unverändert
new Date(),
new Error(),
new Function(),
function () {},
Math,
new Number(),
1, // bleibt unverändert
new Object(),
{},
new RegExp(),
/(?:)/,
new String(),
'test' // bleibt unverändert
];
for (var i = 0; i < types.length; i++) {
types[i].constructor = Type;
types[i] = [types[i].constructor, types[i] instanceof Type, types[i].toString()];
}
console.log(types.join('\n'));
</pre>
<p>Das Beispiel zeigt folgende Ausgabe:</p>
<pre class="brush: js">function Type() {},false,
function Type() {},false,
function Type() {},false,false
function Boolean() {
[native code]
},false,true
function Type() {},false,Mon Sep 01 2014 16:03:49 GMT+0600
function Type() {},false,Error
function Type() {},false,function anonymous() {
}
function Type() {},false,function () {}
function Type() {},false,[object Math]
function Type() {},false,0
function Number() {
[native code]
},false,1
function Type() {},false,[object Object]
function Type() {},false,[object Object]
function Type() {},false,/(?:)/
function Type() {},false,/(?:)/
function Type() {},false,
function String() {
[native code]
},false,тест
</pre>
<h2 id="Specifications" name="Specifications">Spezifikation</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Spezifikation</th>
<th scope="col">Status</th>
<th scope="col">Komentar</th>
</tr>
<tr>
<td>ECMAScript 1 te Edition.</td>
<td>Standard</td>
<td>Ausgangsdefinition: Implementiert in JavaScript 1.1.</td>
</tr>
<tr>
<td>{{SpecName('ES5.1', '#sec-15.2.4.1', 'Object.prototype.constructor')}}</td>
<td>{{Spec2('ES5.1')}}</td>
<td> </td>
</tr>
<tr>
<td>{{SpecName('ES6', '#sec-object.prototype.constructor', 'Object.prototype.constructor')}}</td>
<td>{{Spec2('ES6')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility" name="Browser_compatibility">Browserunterstützung</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>Basis- unterstützung</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</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>Basis- unterstützungt</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
</tr>
</tbody>
</table>
</div>
|