From 074785cea106179cb3305637055ab0a009ca74f2 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:42:52 -0500 Subject: initial commit --- .../reference/global_objects/array/of/index.html | 108 +++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 files/pt-br/web/javascript/reference/global_objects/array/of/index.html (limited to 'files/pt-br/web/javascript/reference/global_objects/array/of') diff --git a/files/pt-br/web/javascript/reference/global_objects/array/of/index.html b/files/pt-br/web/javascript/reference/global_objects/array/of/index.html new file mode 100644 index 0000000000..d7d72259cb --- /dev/null +++ b/files/pt-br/web/javascript/reference/global_objects/array/of/index.html @@ -0,0 +1,108 @@ +--- +title: Array.of() +slug: Web/JavaScript/Reference/Global_Objects/Array/of +tags: + - Array + - ECMAScript6 + - JavaScript + - metodo +translation_of: Web/JavaScript/Reference/Global_Objects/Array/of +--- +
{{JSRef}}
+ +

O método Array.of() cria um nova instância de 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 o construtor de Array é no tratamento dos argumentos inteiros: Array.of(7) cria um array com um único elemento, 7, enquanto Array(7) cria um array vazio de propriedade length igual a 7 (Nota: isso quer dizer um array com 7 espaços vazios, e não com valores do tipo {{jsxref("undefined")}}).

+ +
Array.of(7);       // [7]
+Array.of(1, 2, 3); // [1, 2, 3]
+
+Array(7);          // array com 7 espaços vazios
+Array(1, 2, 3);    // [1, 2, 3]
+
+ +

Syntaxe

+ +
Array.of(element0[, element1[, ...[, elementN]]])
+ +

Parâmetros

+ +
+
elementN
+
Elementos usados para criar o array.
+
+

Valor de retorno

+
+
Uma nova instância de {{jsxref("Array")}}. 
+
+ +

Descrição

+ +

Esta função é parte do padrão ECMAScript 6 (ou ECMAScript 2015).

+ +

Para maiores informações veja:

+ + + +

Exemplos

+ +
Array.of(1);         // [1]
+Array.of(1, 2, 3);   // [1, 2, 3]
+Array.of(undefined); // [undefined]
+
+ +

Polyfill

+ +

Executando o seguinte código antes de qualquer outro c[odigo criará o Array.of() se ele não for disponível nativamente.

+ +
if (!Array.of) {
+  Array.of = function() {
+    return Array.prototype.slice.call(arguments);
+    // Or
+    let vals = [];
+    for(let prop in arguments){
+        vals.push(arguments[prop]);
+    }
+    return vals;
+  };
+}
+
+ +

Especificações

+ + + + + + + + + + + + + + +
EspecificaçãoStatusComentário
{{SpecName('ES6', '#sec-array.of', 'Array.of')}}{{Spec2('ES6')}}Definição inicial.
+ +

Compatibilidade com os navegadores

+ +
+ + +

{{Compat("javascript.builtins.Array.of")}}

+
+ +

Ver também

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