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
|
---
title: Array.prototype.unshift()
slug: Web/JavaScript/Reference/Global_Objects/Array/unshift
tags:
- Array
- JavaScript
- Method
- Prototype
translation_of: Web/JavaScript/Reference/Global_Objects/Array/unshift
---
<div>{{JSRef}}</div>
<p>Die <code><strong>unshift()</strong></code> Methode fügt ein oder mehrere Elemente am Anfang eines Array hinzu und gibt die neue Länge des Arrays zurück.</p>
<div>{{EmbedInteractiveExample("pages/js/array-unshift.html")}}</div>
<h2 id="Syntax">Syntax</h2>
<pre class="syntaxbox"><code><var>arr</var>.unshift(<var>element1</var>[, ...[, <var>elementN</var>]])</code></pre>
<h3 id="Parameter">Parameter</h3>
<dl>
<dt><code>element<em>N</em></code></dt>
<dd>Die Elemente die am Anfang des Arrays hinzugefügt werden sollen.</dd>
</dl>
<h3 id="Rückgabewert">Rückgabewert</h3>
<p>Die neue {{jsxref("Array.length", "length")}} Eigenschaft des Arrays auf dem die Methode aufgerufen wurde.</p>
<h2 id="Beschreibung">Beschreibung</h2>
<p>Die <code>unshift</code> Methode fügt die gegeben Elemente am Anfang eines Arrays ähnlichen Objektes hinzu.</p>
<p><code>unshift</code> ist extra generisch gehalten. Diese Methode kann mit {{jsxref("Function.call", "call", "", 1)}} oder {{jsxref("Function.apply", "apply", "", 1)}} auf einem Array ähnlichen Objekt angewendet werden. Objekte, die nicht über die Eigenschaft <code>length</code> verfügen, welche nicht das letzte in einer Reihe aufeinander folgenden, null-basierenden nummerische Werten repräsentieren, können sinnlose Ergebnisse liefern.</p>
<h2 id="Beispiele">Beispiele</h2>
<pre class="brush: js">var arr = [1, 2];
arr.unshift(0); // result of call is 3, the new array length
// arr is [0, 1, 2]
arr.unshift(-2, -1); // = 5
// arr is [-2, -1, 0, 1, 2]
arr.unshift([-3]);
// arr is [[-3], -2, -1, 0, 1, 2]
</pre>
<h2 id="Spezifikationen">Spezifikationen</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Spezifikation</th>
<th scope="col">Status</th>
<th scope="col">Kommentar</th>
</tr>
<tr>
<td>{{SpecName('ES3')}}</td>
<td>{{Spec2('ES3')}}</td>
<td>Initiale Definition. Implementiert in JavaScript 1.2.</td>
</tr>
<tr>
<td>{{SpecName('ES5.1', '#sec-15.4.4.13', 'Array.prototype.unshift')}}</td>
<td>{{Spec2('ES5.1')}}</td>
<td> </td>
</tr>
<tr>
<td>{{SpecName('ES6', '#sec-array.prototype.unshift', 'Array.prototype.unshift')}}</td>
<td>{{Spec2('ES6')}}</td>
<td> </td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-array.prototype.unshift', 'Array.prototype.unshift')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="Browserkompatibilität">Browserkompatibilität</h2>
<div>
<p>{{Compat("javascript.builtins.Array.unshift")}}</p>
</div>
<h2 id="Siehe_auch">Siehe auch</h2>
<ul>
<li>{{jsxref("Array.prototype.push()")}}</li>
<li>{{jsxref("Array.prototype.pop()")}}</li>
<li>{{jsxref("Array.prototype.shift()")}}</li>
<li>{{jsxref("Array.prototype.concat()")}}</li>
</ul>
|