| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Array/fill | [Back] [Original] |
Get to know MDN better
Esta pgina foi traduzida do ingls pela comunidade. Saiba mais e junte-se comunidade MDN Web Docs.
This feature is well established and works across many devices and browser versions. Its been available across browsers since setembro de 2015.
O mtodo fill() preenche todos os valores do array a partir do ndice inicial a um ndice final com um valor esttico.
arr.fill(valor[, nicio = 0[, fim = this.length]])
O intervalo de preenchimento dos elementos [incio, fim).
O mtodo fill pode receber at trs argumentos valor, nicio e fim. Os argumentos nicio e fim so opcionais com valor padro 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 funo fill intencionalmente genrica, ele no precisa que o valor do this seja um objeto Array.
O mtodo fill um mtodo mutvel, ele ir mudar o objeto em si, e retorn-lo, no somente uma cpia do objeto.
[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}
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;
};
}
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-array.prototype.fill |
[1] Comeando com Chrome 36, isto era disponvel com uma mudana nas preferencias. Em chrome://flags, ativar a entrada "Enable Experimental JavaScript".
This page was last modified on 8 de nov. de 2023 by MDN contributors.
ArrayArray.prototype.at()Array.prototype.concat()Array.prototype.copyWithin()Array.prototype.entries()Array.prototype.every()Array.prototype.fill()Array.prototype.filter()Array.prototype.find()Array.prototype.findIndex()findLast()findLastIndex()Array.prototype.flat()Array.prototype.flatMap()Array.prototype.forEach()Array.prototype.includes()Array.prototype.indexOf()Array.prototype.join()Array.prototype.keys()Array.prototype.lastIndexOf()Array.prototype.map()Array.prototype.pop()Array.prototype.push()Array.prototype.reduce()Array.prototype.reduceRight()Array.prototype.reverse()Array.prototype.shift()Array.prototype.slice()Array.prototype.some()Array.prototype.sort()Array.prototype.splice()Array.prototype.toLocaleString()toReversed()toSorted()toSpliced()Array.prototype.toString()Array.prototype.unshift()Array.prototype.values()with()Array.prototype[@@iterator]()Object/FunctionObject.prototype.__defineGetter__()Object.prototype.__defineSetter__()Object.prototype.__lookupGetter__()Object.prototype.__lookupSetter__()Object.prototype.hasOwnProperty()Object.prototype.isPrototypeOf()Object.prototype.propertyIsEnumerable()Object.prototype.toLocaleString()Object.prototype.toString()Object.prototype.valueOf()Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |