From cb9e359a51c3249d8f5157db69d43fd413ddeda6 Mon Sep 17 00:00:00 2001 From: Florian Merz Date: Thu, 11 Feb 2021 14:45:12 +0100 Subject: unslug ca: move --- .../reference/global_objects/array/fill/index.html | 173 +++++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 files/ca/web/javascript/reference/global_objects/array/fill/index.html (limited to 'files/ca/web/javascript/reference/global_objects/array/fill') diff --git a/files/ca/web/javascript/reference/global_objects/array/fill/index.html b/files/ca/web/javascript/reference/global_objects/array/fill/index.html new file mode 100644 index 0000000000..e1952a8407 --- /dev/null +++ b/files/ca/web/javascript/reference/global_objects/array/fill/index.html @@ -0,0 +1,173 @@ +--- +title: Array.prototype.fill() +slug: Web/JavaScript/Referencia/Objectes_globals/Array/fill +translation_of: Web/JavaScript/Reference/Global_Objects/Array/fill +--- +
{{JSRef}}
+ +

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, posInicialposFinal.

+ +

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óEstatComentaris
{{SpecName('ES6', '#sec-array.prototype.fill', 'Array.prototype.fill')}}{{Spec2('ES6')}}Definició inicial.
+ +

Compatibilitat amb navegadors

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + +
CaracterísticaChromeFirefox (Gecko)Internet ExplorerOperaSafari
Suport bàsic{{CompatChrome("45")}} [1]{{CompatGeckoDesktop("31")}}{{CompatNo}}{{CompatNo}}{{CompatSafari("7.1")}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
CaracterísticaAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Suport bàsic{{CompatNo}}{{CompatNo}}{{CompatGeckoMobile("31")}}{{CompatNo}}{{CompatNo}}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”.

+ +

Vegeu també

+ + -- cgit v1.2.3-54-g00ecf