--- title: Array.prototype.fill() slug: Web/JavaScript/Reference/Global_Objects/Array/fill translation_of: Web/JavaScript/Reference/Global_Objects/Array/fill original_slug: Web/JavaScript/Referencje/Obiekty/Array/fill ---
Metoda fill() uzupełnia wszystkie elementy tablicy, zaczynając od indeksu początkowego (start) aż po indeks końcowy (end) statyczną wartością (value).
{{EmbedInteractiveExample("pages/js/array-fill.html")}}
Źródło tego przykładu jest przechowywane w repozytorium na GitHub. Jeśli chciałbyś dodać coś od siebie do projektu interaktywnych przykładów, sklonuj https://github.com/mdn/interactive-examples i wyślij pull request.
arr.fill(value[, start = 0[, end = this.length]])
valuestartendZmodyfikowana tablica.
Przedział elementów do wypełnienia to: [start, end).
Metoda fill przyjmuje do trzech parametrów value, start i end. Argumenty start i end są opcjonalne i przyjmują, odpowiednio, 0 i długość (length) obiektu this.
Jeżeli parametr start jest ujemny, jest to traktowane jako length+start gdzie length jest liczbą elementów tablicy. Jeżeli parametr end jest negatywny, jest to traktowane jako length+end.
Funkcja fill została świdomie zaprojektowana jako generyczna, przez co nie wymaga, by wartość this była obiektem typu Array.
Metoda fill jest zmienna (ang. mutalbe), metoda ta nie zwraca kopii this, a oryginalny obiekt po modyfikacjach.
[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, -2); // [4, 2, 3]
[1, 2, 3].fill(4, NaN, NaN); // [1, 2, 3]
[].fill.call({ length: 3 }, 4); // {0: 4, 1: 4, 2: 4, length: 3}
//Obiekty przez referencję
var arr = Array(3).fill({}) // [{}, {}, {}];
arr[0].hi = "hi"; // [{ hi: "hi" }, { hi: "hi" }, { hi: "hi" }]
if (!Array.prototype.fill) {
Array.prototype.fill = 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 >>> 0;
// Steps 6-7.
var start = arguments[1];
var relativeStart = start >> 0;
// Step 8.
var k = relativeStart < 0 ?
Math.max(len + relativeStart, 0) :
Math.min(relativeStart, len);
// Steps 9-10.
var end = arguments[2];
var relativeEnd = end === undefined ?
len : end >> 0;
// Step 11.
var final = relativeEnd < 0 ?
Math.max(len + relativeEnd, 0) :
Math.min(relativeEnd, len);
// Step 12.
while (k < final) {
O[k] = value;
k++;
}
// Step 13.
return O;
};
}
| Specyfikacja | Status | Komentarz |
|---|---|---|
| {{SpecName('ES6', '#sec-array.prototype.fill', 'Array.prototype.fill')}} | {{Spec2('ES6')}} | Definicja początkowa |
| Funckjonalność | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Wsparcie podstawowe | {{CompatChrome("36")}} [1] | {{CompatGeckoDesktop("31")}} | {{CompatNo}} | {{CompatNo}} | {{CompatSafari("7.1")}} |
| Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Wsparcie podstawowe | {{CompatNo}} | {{CompatNo}} | {{CompatGeckoMobile("31")}} | {{CompatNo}} | {{CompatNo}} | 8.0 |
[1] The feature is available behind a preference. In chrome://flags, activate the entry “Enable Experimental JavaScript”.