--- title: Array.of() slug: Web/JavaScript/Reference/Global_Objects/Array/of translation_of: Web/JavaScript/Reference/Global_Objects/Array/of original_slug: Web/JavaScript/Referencia/Objectes_globals/Array/of ---
El mètode Array.of() crea una nova instància Array amb un nombre variable d'arguments, sense tenir en compte el nombre o el tipus d'arguments.
La diferència entre Array.of() i el constructor Array es troba en el maneig dels arguments sencers: Array.of(42) crea un array amb un sol element, 42, mentre que Array(42) crea un array amb 42 elements, Cadascun dels quals és undefined.
Array.of(element0[, element1[, ...[, elementN]]])
elementNAquesta funció forma part del ECMAScript 6 estàndard. Per més informació vegeu proposta de l'Array.of i Array.from i Array.of polyfill.
Array.of(1); // [1] Array.of(1, 2, 3); // [1, 2, 3] Array.of(undefined); // [undefined]
Executar el codi següent abans que cap altre codi crearà Array.of() en cas que no es trobi disponible de forma nativa.
if (!Array.of) {
Array.of = function() {
return Array.prototype.slice.call(arguments);
};
}
| Especificació | Estat | Comentaris |
|---|---|---|
| {{SpecName('ES6', '#sec-array.of', 'Array.of')}} | {{Spec2('ES6')}} | Definició inicial. |
| Característica | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Suport bàsic | {{CompatChrome(45)}} | {{CompatGeckoDesktop("25")}} | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} |
| Característica | Android | Chrome per Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Suport bàsic | {{CompatNo}} | {{CompatChrome(39)}} | {{CompatGeckoMobile("25")}} | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} |