aboutsummaryrefslogtreecommitdiff
path: root/files/de/web/javascript/reference/global_objects/array/fill/index.html
blob: 5f4821e986dceae13517fb8666e282cd9bb47e59 (plain)
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
---
title: Array.prototype.fill()
slug: Web/JavaScript/Reference/Global_Objects/Array/fill
tags:
  - Array
  - ECMAScript 2015
  - JavaScript
  - Method
  - Prototype
  - polyfill
translation_of: Web/JavaScript/Reference/Global_Objects/Array/fill
---
<div>{{JSRef}}</div>

<p>Die <code><strong>fill()</strong></code> Methode befüllt ein Array mit einem statischen Wert von einem Startindex bis zu einem Endindex. Der Endindex wird nicht mit eingeschlossen.</p>

<div>{{EmbedInteractiveExample("pages/js/array-fill.html")}}</div>



<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox"><code><var>arr</var>.fill(<var>value</var>[, <var>start<var>[, <var>end</var>]])</var></var></code></pre>

<h3 id="Parameter">Parameter</h3>

<dl>
 <dt><code>value</code></dt>
 <dd>Wert, mit dem ein Array gefüllt werden soll.</dd>
 <dt><code>start</code> {{optional_inline}}</dt>
 <dd>Startindex, Standardwert ist 0.</dd>
 <dt><code>end</code> {{optional_inline}}</dt>
 <dd>Endindex, Standardwert ist <code>this.length</code>.</dd>
</dl>

<h3 id="Rückgabewert">Rückgabewert</h3>

<p>Das geänderte Array.</p>

<h2 id="Beschreibung">Beschreibung</h2>

<p>Die <strong><code>fill</code></strong> Methode nimmt bis zu drei Argumente entgegen: <code>value</code>, <code>start</code> und <code>end</code>. Die Argumente <code>start</code> und <code>end</code> sind optional und haben als Standardwert 0 und <code>length</code> des <code>this</code> Objektes.</p>

<p>Wenn <code>start</code> negativ ist, wird stattdessen <code>length+start</code> benutzt, wobei <code>length</code> die Länge des Arrays ist. Wenn <code>end</code> negativ ist, wird stattdessen <code>length+end</code> benutzt.</p>

<p>Die <code><strong>fill</strong></code> Funktion ist absichtlich generisch. Es ist nicht nötig, dass der <code>this</code> Wert ein Array Objekt ist.</p>

<p>Die <code><strong>fill</strong></code> Methode ist eine verändernde Methode. Sie verändert das <code>this</code> Objekt selbst und gibt dieses zurück. Sie erstellt keine Kopie des Objektes.</p>

<p>Wenn der <code><strong>fill</strong></code> Methode ein Objekt übergeben wird, wird das Objekt kopiert und die Referenz der Kopie wird im Array eingesetzt.</p>

<h2 id="Beispiele">Beispiele</h2>

<pre class="brush: js">[1, 2, 3].fill(4);               // [4, 4, 4]
[1, 2, 3].fill(4, 1);            // [1, 4, 4]
[1, 2, 3].fill(4, 1, 2);         // [1, 4, 3]
[1, 2, 3].fill(4, 1, 1);         // [1, 2, 3]
[1, 2, 3].fill(4, 3, 3);         // [1, 2, 3]
[1, 2, 3].fill(4, -3, -2);       // [4, 2, 3]
[1, 2, 3].fill(4, NaN, NaN);     // [1, 2, 3]
[1, 2, 3].fill(4, 3, 5);         // [1, 2, 3]
Array(3).fill(4);                // [4, 4, 4]
[].fill.call({ length: 3 }, 4);  // {0: 4, 1: 4, 2: 4, length: 3}

// Objects by reference.
var arr = Array(3).fill({}) // [{}, {}, {}];
arr[0].hi = "hi"; // [{ hi: "hi" }, { hi: "hi" }, { hi: "hi" }]
</pre>

<h2 id="Polyfill">Polyfill</h2>

<pre class="brush: js">if (!Array.prototype.fill) {
  Object.defineProperty(Array.prototype, 'fill', {
    value: function(value) {

      // Steps 1-2.
      if (this == null) {
        throw new TypeError('this is null or not defined');
      }

      var O = Object(this);

      // Steps 3-5.
      var len = O.length &gt;&gt;&gt; 0;

      // Steps 6-7.
      var start = arguments[1];
      var relativeStart = start &gt;&gt; 0;

      // Step 8.
      var k = relativeStart &lt; 0 ?
        Math.max(len + relativeStart, 0) :
        Math.min(relativeStart, len);

      // Steps 9-10.
      var end = arguments[2];
      var relativeEnd = end === undefined ?
        len : end &gt;&gt; 0;

      // Step 11.
      var final = relativeEnd &lt; 0 ?
        Math.max(len + relativeEnd, 0) :
        Math.min(relativeEnd, len);

      // Step 12.
      while (k &lt; final) {
        O[k] = value;
        k++;
      }

      // Step 13.
      return O;
    }
  });
}
</pre>

<p>Wenn es wirklich notwendig ist veraltete JavaScript-Umgebungen zu unterstützen, die <code><a href="/de/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty">Object.defineProperty</a></code> nicht unterstützen, ist es meistens besser Methoden von <code>Array.prototype</code> nicht mit einem Polyfill zu unterstützen, weil sie nicht als nicht-aufzählbar eingestellt werden können.</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">Kommentar</th>
  </tr>
  <tr>
   <td>{{SpecName('ES2015', '#sec-array.prototype.fill', 'Array.prototype.fill')}}</td>
   <td>{{Spec2('ES2015')}}</td>
   <td>Initiale Definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-array.prototype.fill', 'Array.prototype.fill')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="Browserkompatibilität">Browserkompatibilität</h2>

<div>


<p>{{Compat("javascript.builtins.Array.fill")}}</p>
</div>

<h2 id="Siehe_auch">Siehe auch</h2>

<ul>
 <li>{{jsxref("Array")}}</li>
 <li>{{jsxref("TypedArray.prototype.fill()")}}</li>
</ul>