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
|
---
title: Array.isArray()
slug: Web/JavaScript/Reference/Global_Objects/Array/isArray
tags:
- Array
- ECMAScript5
- JavaScript
- Method
- Reference
- polyfill
translation_of: Web/JavaScript/Reference/Global_Objects/Array/isArray
---
<div>{{JSRef}}</div>
<p>Die <code><strong>Array.isArray()</strong></code> Funktion prüft, ob das übergebene Objekt ein {{jsxref("Array")}} ist.</p>
<pre class="brush: js">Array.isArray([1, 2, 3]); // true
Array.isArray({foo: 123}); // false
Array.isArray('foobar'); // false
Array.isArray(undefined); // false
</pre>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox"><code>Array.isArray(<var>obj</var>)</code></pre>
<h3 id="Parameters" name="Parameters">Parameter</h3>
<dl>
<dt><code>value</code></dt>
<dd>Der zu überprüfende Wert.</dd>
</dl>
<h3 id="Rückgabewert">Rückgabewert</h3>
<p>Wenn der Wert ein {{jsxref("Array")}} ist wird <code>true</code> zurückgegeben, andernfalls <code>false</code>.</p>
<h2 id="Description" name="Description">Beschreibung</h2>
<p>Wenn der Wert ein {{jsxref("Array")}} ist, wir <code>true</code> zurückzugeben, andernfalls <code>false</code>.</p>
<p>Eine detailliertere Beschreibung ist im Artikel <a href="http://web.mit.edu/jwalden/www/isArray.html">Determining with absolute accuracy whether or not a JavaScript object is an array</a> enthalten (auf Englisch). Wird eine {{jsxref("TypedArray")}}-Instanz geprüft, wird immer <code>false</code> zurückgegeben.</p>
<h2 id="Beispiele">Beispiele</h2>
<h3 id="Einsatz_von_Array.isArray">Einsatz von Array.isArray</h3>
<pre class="brush: js">// die folgenden Ausdrücke geben true zurück
Array.isArray([]);
Array.isArray([1]);
Array.isArray(new Array());
Array.isArray(new Array('a', 'b', 'c', 'd'));
Array.isArray(new Array(3));
// wenig bekannt: Array.prototype ist selbst ein Array.
Array.isArray(Array.prototype);
// die folgenden Ausdrücke geben alle false zurück
Array.isArray();
Array.isArray({});
Array.isArray(null);
Array.isArray(undefined);
Array.isArray(17);
Array.isArray("Array");
Array.isArray(true);
Array.isArray(false);
Array.isArray(new Uint8Array(32));
Array.isArray({ __proto__ : Array.prototype });
</pre>
<h3 id="instanceof_vs_isArray"><code>instanceof</code> vs <code>isArray</code></h3>
<p>Wenn auf eine <code>Array</code> Instanz geprüft wird, ist <code>Array.isArray</code> besser geeignet als <code>instanceof</code>, weil es auch mit <code>iframes</code> funktioniert.</p>
<pre class="brush: js">var iframe = document.createElement('iframe');
document.body.appendChild(iframe);
xArray = window.frames[window.frames.length-1].Array;
var arr = new xArray(1,2,3); // [1,2,3]
// Richtiges Prüfen für Arrays
Array.isArray(arr); // true
// Als nicht richtig angesehen, weil es nicht mit iframes funktioniert
arr instanceof Array; // false
</pre>
<h2 id="Compatibility" name="Compatibility">Polyfill</h2>
<p>Der folgende Code implementiert die Methode, wenn <code>Array.isArray()</code> nicht nativ unterstützt wird.</p>
<pre class="brush: js">if(!Array.isArray) {
Array.isArray = function (vArg) {
return Object.prototype.toString.call(vArg) === "[object Array]";
};
}
</pre>
<h2 id="Spezifikationen">Spezifikationen</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Spezifikation</th>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-array.isarray', 'Array.isArray')}}</td>
</tr>
</tbody>
</table>
<h2 id="Browserkompatibilität">Browserkompatibilität</h2>
<div>
<p>{{Compat("javascript.builtins.Array.isArray")}}</p>
</div>
<h2 id="Siehe_auch">Siehe auch</h2>
<ul>
<li>{{jsxref("Array")}}</li>
</ul>
|