--- title: Array.prototype.fill() slug: Web/JavaScript/Reference/Global_Objects/Array/fill translation_of: Web/JavaScript/Reference/Global_Objects/Array/fill ---
Il metodo fill() assegna un valore statico a tutti gli elementi di un array compresi tra un indice iniziale ed un indice finale.
var numbers = [1, 2, 3] numbers.fill(1); // results in [1, 1, 1]
arr.fill(value) arr.fill(value, start) arr.fill(value, start, end)
valuestart {{optional_inline}}end {{optional_inline}}this.length.L'array modificato.
L'intervallo di elementi da assegnare è compreso tra [start, end).
Il metodo fill accetta fino a tre argomenti: value, start and end. Gli argomenti start e end sono opzionali con valori di default rispettivamente di 0 di length dell'oggetto this .
Se start è negativo, viene interpretato come length+start dove length è la lunghezza dell'array. Se end è negativo, viene intrpretato come length+end.
La funzione fill è volutamente generica, non richiede che il suo valore this sia un Array.
Il metodo fill è mutabile, ovvero modifica l'oggetto this e lo restituisce modificato, ovvero non ne restituisce una copia modificata.
Quando al metodo fill viene passato un oggetto, il metodo effettuerà una copia dell'oggetto e assegnerà agli elementi dell'Array un riferimento alla copia.
[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, 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" }]
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 >>> 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;
}
});
}
Se hai necessità di supportare engine Javascript veramente obsolete che non supportano Object.defineProperty, è meglio non usare affatto il polyfill per il medoto Array.prototype perchè non è possibile renderlo non enumerabile.
| Specifica | Stato | Commenti |
|---|---|---|
| {{SpecName('ES2015', '#sec-array.prototype.fill', 'Array.prototype.fill')}} | {{Spec2('ES2015')}} | Initial definition. |
| {{SpecName('ESDraft', '#sec-array.prototype.fill', 'Array.prototype.fill')}} | {{Spec2('ESDraft')}} |
{{Compat("javascript.builtins.Array.fill")}}