This is a new technology, part of the ECMAScript 2015 (ES6) standard.
This technology's specification has been finalized, but check the compatibility table for usage and implementation status in various browsers.
O método Array.of() cria um nova instancia do Array com um número variável de argumentos, independentemente do número ou do tipo dos argumentos.
A diferença entre o Array.of() e os construtores do Array é no tratamento dos argumentos inteiros: Array.of(42) cria um array com um único elemento, 42, enquanto Array(42) cria um array com 42 elementos, cada um com valor undefined.
Sintaxe
Array.of(element0[, element1[, ...[, elementN]]])
Parâmetros
elementN- Elemento a qual será criado o array.
Descrição
Esta função é parte do padrão ECMAScript 6. Para maiores informações ver Array.of and Array.from proposal e Array.of polyfill.
Exemplos
Array.of(1); // [1] Array.of(1, 2, 3); // [1, 2, 3] Array.of(undefined); // [undefined]
Polyfill
Executar o seguinte codigo antes de qualquer outro codigo criará o Array.of() se o mesmo não for disponível nativamente.
if (!Array.of) {
Array.of = function() {
return Array.prototype.slice.call(arguments);
};
}
Especificações
| Especificação | Status | Comentário |
|---|---|---|
| ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Array.of' in that specification. |
Standard | Definição inicial. |
Compatibilidade com os navegadores
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Suporte básico | 45 | 25 (25) | Não suportado | Não suportado | Não suportado |
| Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Suporte básico | Não suportado | 39 | 25.0 (25) | Não suportado | Não suportado | Não suportado |