This is a new technology, part of the ECMAScript 2015 (ES6) standard.
This technology's specification has been finalized, but check the compatibility table for usage and implementation status in various browsers.
El mètode fill() omple tots els elements d'un array a partir d'una posició inicial fins a una posició final amb un valor estàtic predeterminat.
Sintaxi
arr.fill(valor[, posInicial = 0[, posFinal = this.length]])
Paràmetres
valor- Valor amb el que s'omplirà l'array.
posInicial- Opcional. Posició inicial.
posFinal- Opcional. Posició final.
Descripció
L'interval d'elements a omplir és [posInicial, posFinal) (inici inclusiu, final exclusiu).
El mètode fill accepta fins a tres arguments: valor, posInicial i posFinal.
Els arguments posInicial i posFinal són opcionals i si no s'especifiquen prenen per defecte els valors 0 i la propietat length de l'objecte this, respectivament.
Si posInicial és negatiu, es considera com a length+start on length és la mida de l'array. Si posFinal és negatiu es considera com a length+end.
La funció fill és genèrica intencionalment i no requereix que el valor this sigui un objecte de tipus Array.
El mètode fill és mutable, ja que canviarà l'objecte this en si mateix i després el retornarà com a resultat, en comptes de retornar una copia d'aquest.
Exemples
[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]
Array(3).fill(4); // [4, 4, 4]
[].fill.call({ length: 3 }, 4); // {0: 4, 1: 4, 2: 4, length: 3}
Polyfill
if (!Array.prototype.fill) {
Array.prototype.fill = function(value) {
// Pasos 1-2.
if (this == null) {
throw new TypeError('this is null or not defined');
}
var O = Object(this);
// Pasos 3-5.
var len = O.length >>> 0;
// Pasos 6-7.
var start = arguments[1];
var relativeStart = start >> 0;
// Pasos 8.
var k = relativeStart < 0 ?
Math.max(len + relativeStart, 0) :
Math.min(relativeStart, len);
// Pasos 9-10.
var end = arguments[2];
var relativeEnd = end === undefined ?
len : end >> 0;
// Pasos 11.
var final = relativeEnd < 0 ?
Math.max(len + relativeEnd, 0) :
Math.min(relativeEnd, len);
// Pasos 12.
while (k < final) {
O[k] = value;
k++;
}
// Pasos 13.
return O;
};
}
Especificacions
| Especificació | Estat | Comentaris |
|---|---|---|
| ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Array.prototype.fill' in that specification. |
Standard | Definició inicial. |
Compatibilitat amb navegadors
| Característica | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Suport bàsic | 45 [1] | 31 (31) | Not supported | Not supported | 7.1 |
| Característica | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Suport bàsic | Not supported | Not supported | 31.0 (31) | Not supported | Not supported | 8.0 |
[1] A partir del Chrome 36, està disponible a través d'una preferència. A chrome://flags, activeu l'entrada “Enable Experimental JavaScript”.