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
.
Sintaxi
Array.of(element0[, element1[, ...[, elementN]]])
Paràmetres
elementN
- Elements a partir dels quals es crea l'array.
Descripció
Aquesta 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.
Exemples
Array.of(1); // [1]
Array.of(1, 2, 3); // [1, 2, 3]
Array.of(undefined); // [undefined]
Polyfill
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);
};
}
Especificacions
Especificació | Estat | Comentaris |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Array.of' in that specification. |
Standard | Definició inicial. |
Compatibilitat amb navegadors
We're converting our compatibility data into a machine-readable JSON format.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
Find out how you can help!
Característica | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Suport bàsic | 45 | 25 (25) | No support | No support | No support |
Característica | Android | Chrome per Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Suport bàsic | No support | 39 | 25.0 (25) | No support | No support | No support |