Baseline Widely available
O método fill()
preenche todos os valores do array a partir do Ãndice inicial a um Ãndice final com um valor estático.
arr.fill(valor[, Ãnicio = 0[, fim = this.length]])Parâmetros
valor
Valor para preencher o array.
Ãnicio
Opcional. Ãndice inicial.
fim
Opcional. Ãndice final.
O intervalo de preenchimento dos elementos é [inÃcio
, fim
).
O método fill
pode receber até três argumentos valor
, Ãnicio
e fim
. Os argumentos Ãnicio
e fim
são opcionais com valor padrão 0 (valor)
e o tamanho do objeto (fim)
.
Se o Ãnicio
for negativo, ele será tratado como tamanho + Ãnicio
onde tamanho é o tamanho total do array
. Se o fim
for negativo, ele será tratado como tamanho + fim
.
A função fill é intencionalmente genérica, ele não precisa que o valor do this seja um objeto Array.
O método fill é um método mutável, ele irá mudar o objeto em si, e retorná-lo, não somente uma cópia do objeto.
Exemplos[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) {
// Passo 1-2.
if (this == null) {
throw new TypeError("this is null or not defined");
}
var O = Object(this);
// Passo 3-5.
var len = O.length >>> 0;
// Passo 6-7.
var start = arguments[1];
var relativeStart = start >> 0;
// Passo 8.
var k =
relativeStart < 0
? Math.max(len + relativeStart, 0)
: Math.min(relativeStart, len);
// Passo 9-10.
var end = arguments[2];
var relativeEnd = end === undefined ? len : end >> 0;
// Passo 11.
var final =
relativeEnd < 0
? Math.max(len + relativeEnd, 0)
: Math.min(relativeEnd, len);
// Passo 12.
while (k < final) {
O[k] = value;
k++;
}
// Passo 13.
return O;
};
}
Especificações Compatibilidade com navegadores
[1] Começando com Chrome 36, isto era disponÃvel com uma mudança nas preferencias. Em chrome://flags, ativar a entrada "Enable Experimental JavaScript".
Ver tambémRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4