1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
---
title: Int16Array
slug: Web/JavaScript/Reference/Global_Objects/Int16Array
tags:
- Construtor
- JavaScript
- TypedArray
- TypedArrays
translation_of: Web/JavaScript/Reference/Global_Objects/Int16Array
---
<div>{{JSRef}}</div>
<p> </p>
<p>A matriz do tipo <strong><code>Int16Array</code></strong> representa uma matriz de inteiros assinados de 16 bits com dois complementos na ordem de bytes da plataforma. Se o controle sobre a ordem de bytes for necessário, use {{jsxref ("DataView")}}. O conteúdo é inicializado em 0. Uma vez estabelecido, você pode fazer referência a elementos na matriz usando os métodos do objeto ou usando a sintaxe de índice da matriz padrão (ou seja, usando a notação de colchetes).</p>
<h2 id="Sintaxe">Sintaxe</h2>
<pre class="syntaxbox">new Int16Array(); // novo no ES2017
new Int16Array(length);
new Int16Array(typedArray);
new Int16Array(object);
new Int16Array(buffer [, byteOffset [, length]]);</pre>
<p> </p>
<p>Para obter mais informações sobre a sintaxe do construtor e os parâmetros, consulte <em><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#Syntax">TypedArray</a></em>.</p>
<h2 id="Propriedades">Propriedades</h2>
<dl>
<dt>{{jsxref("TypedArray.BYTES_PER_ELEMENT", "Int16Array.BYTES_PER_ELEMENT")}}</dt>
<dd>Retorna um valor numérico do tamanho do elemento. <code>2</code> no caso de um <code>Int16Array</code>.</dd>
<dt>Int16Array.length</dt>
<dd>Propriedade de comprimento estático cujo valor é 0. Para o comprimento real (número de elementos), consulte {{jsxref("TypedArray.prototype.length", "Int16Array.prototype.length")}}.</dd>
<dt>{{jsxref("TypedArray.name", "Int16Array.name")}}</dt>
<dd>Retorna o valor da string do nome do construtor. No caso do tipo <code>Int16Array</code>: "Int16Array".</dd>
<dt>{{jsxref("TypedArray.prototype", "Int16Array.prototype")}}</dt>
<dd>Protótipo para os objetos TypedArray.</dd>
</dl>
<h2 id="Métodos">Métodos</h2>
<dl>
<dt>{{jsxref("TypedArray.from", "Int16Array.from()")}}</dt>
<dd>Cria um novo <code>Int16Array</code> de um objeto semelhante a uma matriz ou iterável. Veja também {{jsxref("Array.from()")}}.</dd>
<dt>{{jsxref("TypedArray.of", "Int16Array.of()")}}</dt>
<dd>Cria um novo <code>Int16Array</code> com um número variável de argumentos. Veja também {{jsxref("Array.of()")}}.</dd>
</dl>
<h2 id="Int16Array_prototype"><code>Int16Array</code> prototype</h2>
<p>Todos <code>Int16Array</code> objetos herdam de {{jsxref("TypedArray.prototype", "%TypedArray%.prototype")}}.</p>
<h3 id="Propriedades_2">Propriedades</h3>
<dl>
<dt><code>Int16Array.prototype.constructor</code></dt>
<dd>Retorna a função que criou o protótipo de uma instância. Este é o construtor <code>Int16Array</code> por padrão.</dd>
<dt>{{jsxref("TypedArray.prototype.buffer", "Int16Array.prototype.buffer")}} {{readonlyInline}}</dt>
<dd>Retorna o {{jsxref("ArrayBuffer")}} referenciado pelo <code>Int16Array</code> Fixado em tempo de construção e <strong>apenas leitura</strong>.</dd>
<dt>{{jsxref("TypedArray.prototype.byteLength", "Int16Array.prototype.byteLength")}} {{readonlyInline}}</dt>
<dd>Retorna o tamanho (em bytes) de um <code>Int16Array</code> desde o ínicio {{jsxref("ArrayBuffer")}}. Fixado em tempo de construção e <strong>apenas leitura</strong>.</dd>
<dt>{{jsxref("TypedArray.prototype.byteOffset", "Int16Array.prototype.byteOffset")}} {{readonlyInline}}</dt>
<dd>Retorna o deslocamento (em bytes) de um <code>Int16Array</code> desde o ínicio {{jsxref("ArrayBuffer")}}. Fixado em tempo de construção e <strong>apenas leitura</strong>.</dd>
<dt>{{jsxref("TypedArray.prototype.length", "Int16Array.prototype.length")}} {{readonlyInline}}</dt>
<dd>Retorna o número de elementos em um <code>Int16Array</code>. Fixado em tempo de construção e <strong>apenas leitura</strong>.</dd>
</dl>
<h3 id="Métodos_2">Métodos</h3>
<dl>
<dt>{{jsxref("TypedArray.copyWithin", "Int16Array.prototype.copyWithin()")}}</dt>
<dd>Copia uma sequência de elementos da matriz dentro da matriz. Veja também {{jsxref("Array.prototype.copyWithin()")}}.</dd>
<dt>{{jsxref("TypedArray.entries", "Int16Array.prototype.entries()")}}</dt>
<dd>Retorna um novo <code>Array Iterator</code> objeto que contém os pares de chave / valor para cada índice na matriz. Veja também {{jsxref("Array.prototype.entries()")}}.</dd>
<dt>{{jsxref("TypedArray.every", "Int16Array.prototype.every()")}}</dt>
<dd>Testa se todos os elementos na matriz passam no teste fornecido por uma função. Veja também {{jsxref("Array.prototype.every()")}}.</dd>
<dt>{{jsxref("TypedArray.fill", "Int16Array.prototype.fill()")}}</dt>
<dd>Preenche todos os elementos de uma matriz de um índice inicial para um índice final com um valor estático. Veja também {{jsxref("Array.prototype.fill()")}}.</dd>
<dt>{{jsxref("TypedArray.filter", "Int16Array.prototype.filter()")}}</dt>
<dd>Cria uma nova matriz com todos os elementos dessa matriz para os quais a função de filtragem fornecida retorna true. Veja também {{jsxref("Array.prototype.filter()")}}.</dd>
<dt>{{jsxref("TypedArray.find", "Int16Array.prototype.find()")}}</dt>
<dd>Retorna o valor encontrado na matriz, se um elemento na matriz satisfizer a função de teste fornecida ou <code>undefined</code> se não encontrado. Veja também {{jsxref("Array.prototype.find()")}}.</dd>
<dt>{{jsxref("TypedArray.findIndex", "Int16Array.prototype.findIndex()")}}</dt>
<dd>Retorna o índice encontrado na matriz, se um elemento na matriz satisfizer a função de teste fornecida ou -1, se não for encontrado. Veja também {{jsxref("Array.prototype.findIndex()")}}.</dd>
<dt>{{jsxref("TypedArray.forEach", "Int16Array.prototype.forEach()")}}</dt>
<dd>Chama uma função para cada elemento na matriz. Veja também {{jsxref("Array.prototype.forEach()")}}.</dd>
<dt>{{jsxref("TypedArray.includes", "Int16Array.prototype.includes()")}} {{experimental_inline}}</dt>
<dd>Determina se uma matriz tipificada inclui um determinado elemento, retornando <code>true</code> ou <code>false</code>. Veja também {{jsxref("Array.prototype.includes()")}}.</dd>
<dt>{{jsxref("TypedArray.indexOf", "Int16Array.prototype.indexOf()")}}</dt>
<dd>Retorna o primeiro (menos) índice de um elemento dentro da matriz igual ao valor especificado ou -1 se nenhum for encontrado. Veja também {{jsxref("Array.prototype.indexOf()")}}.</dd>
<dt>{{jsxref("TypedArray.join", "Int16Array.prototype.join()")}}</dt>
<dd>Junta todos os elementos de um array em uma string. Veja também {{jsxref("Array.prototype.join()")}}.</dd>
<dt>{{jsxref("TypedArray.keys", "Int16Array.prototype.keys()")}}</dt>
<dd>Retorna um novo <code>Array Iterator</code> que contém uma chave para cada índice no array. Veja também {{jsxref("Array.prototype.keys()")}}.</dd>
<dt>{{jsxref("TypedArray.lastIndexOf", "Int16Array.prototype.lastIndexOf()")}}</dt>
<dd>Retorna o último (maior) índice de um elemento dentro da matriz igual ao valor especificado ou -1 se nenhum for encontrado. Veja também {{jsxref("Array.prototype.lastIndexOf()")}}.</dd>
<dt>{{jsxref("TypedArray.map", "Int16Array.prototype.map()")}}</dt>
<dd>Cria uma nova matriz com os resultados da chamada de uma função fornecida em todos os elementos dessa matriz. Veja também {{jsxref("Array.prototype.map()")}}.</dd>
<dt>{{jsxref("TypedArray.move", "Int16Array.prototype.move()")}} {{non-standard_inline}} {{unimplemented_inline}}</dt>
<dd>Versão não-padrão anterior de {{jsxref("TypedArray.copyWithin", "Int16Array.prototype.copyWithin()")}}.</dd>
<dt>{{jsxref("TypedArray.reduce", "Int16Array.prototype.reduce()")}}</dt>
<dd>Aplique uma função contra um acumulador e cada valor da matriz (da esquerda para a direita) para reduzi-lo a um único valor. Veja também {{jsxref("Array.prototype.reduce()")}}.</dd>
<dt>{{jsxref("TypedArray.reduceRight", "Int16Array.prototype.reduceRight()")}}</dt>
<dd>Aplique uma função contra um acumulador e cada valor da matriz (da direita para a esquerda) para reduzi-lo a um único valor. Veja também {{jsxref("Array.prototype.reduceRight()")}}.</dd>
<dt>{{jsxref("TypedArray.reverse", "Int16Array.prototype.reverse()")}}</dt>
<dd>Inverte a ordem dos elementos de um array - o primeiro torna-se o último e o último torna-se o primeiro. Veja também {{jsxref("Array.prototype.reverse()")}}.</dd>
<dt>{{jsxref("TypedArray.set", "Int16Array.prototype.set()")}}</dt>
<dd>Armazena vários valores na matriz tipada, lendo valores de entrada de uma matriz especificada.</dd>
<dt>{{jsxref("TypedArray.slice", "Int16Array.prototype.slice()")}}</dt>
<dd>Extrai uma seção de uma matriz e retorna uma nova matriz. Veja também {{jsxref("Array.prototype.slice()")}}.</dd>
<dt>{{jsxref("TypedArray.some", "Int16Array.prototype.some()")}}</dt>
<dd>Retorna true se pelo menos um elemento nessa matriz satisfizer a função de teste fornecida. Veja também {{jsxref("Array.prototype.some()")}}.</dd>
<dt>{{jsxref("TypedArray.sort", "Int16Array.prototype.sort()")}}</dt>
<dd>Classifica os elementos de uma matriz e retorna a matriz. Veja também {{jsxref("Array.prototype.sort()")}}.</dd>
<dt>{{jsxref("TypedArray.subarray", "Int16Array.prototype.subarray()")}}</dt>
<dd>Retorna um novo <code>Int16Array </code>a partir do índice de elemento inicial e final fornecido.</dd>
<dt>{{jsxref("TypedArray.values", "Int16Array.prototype.values()")}}</dt>
<dd>Retorna um novo objeto <code>Array Iterator</code> que contém os valores para cada índice na matriz. Veja também {{jsxref("Array.prototype.values()")}}.</dd>
<dt>{{jsxref("TypedArray.toLocaleString", "Int16Array.prototype.toLocaleString()")}}</dt>
<dd>Retorna uma string localizada representando a matriz e seus elementos. Veja também {{jsxref("Array.prototype.toLocaleString()")}}.</dd>
<dt>{{jsxref("TypedArray.toString", "Int16Array.prototype.toString()")}}</dt>
<dd>Retorna uma string representando a matriz e seus elementos. Veja também {{jsxref("Array.prototype.toString()")}}.</dd>
<dt>{{jsxref("TypedArray.@@iterator", "Int16Array.prototype[@@iterator]()")}}</dt>
<dd>Retorna um novo objeto <code>Array Iterator</code> que contém os valores para cada índice na lista.</dd>
</dl>
<h2 id="Exemplos">Exemplos</h2>
<p>Diferentes opções de criar um <code>Int16Array</code>:</p>
<pre class="brush: js">// De um tamanho
var int16 = new Int16Array(2);
int16[0] = 42;
console.log(int16[0]); // 42
console.log(int16.length); // 2
console.log(int16.BYTES_PER_ELEMENT); // 2
// De uma array
var arr = new Int16Array([21,31]);
console.log(arr[1]); // 31
// De um outro TypedArray
var x = new Int16Array([21, 31]);
var y = new Int16Array(x);
console.log(y[0]); // 21
// De um ArrayBuffer
var buffer = new ArrayBuffer(8);
var z = new Int16Array(buffer, 0, 4);
// De um interável
var iterable = function*(){ yield* [1,2,3]; }();
var int16 = new Int16Array(iterable);
// Int16Array[1, 2, 3]
</pre>
<h2 id="Especificações">Especificações</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Especificação</th>
<th scope="col">Status</th>
<th scope="col">Comentário</th>
</tr>
<tr>
<td>{{SpecName('Typed Array')}}</td>
<td>{{Spec2('Typed Array')}}</td>
<td>Substituído pelo ECMAScript 2015.</td>
</tr>
<tr>
<td>{{SpecName('ES2015', '#table-49', 'TypedArray constructors')}}</td>
<td>{{Spec2('ES2015')}}</td>
<td>Definição inicial em um padrão ECMA. Especificado <code>new</code> como requerido.</td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#table-49', 'TypedArray constructors')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td>ECMAScript 2017 mudou o construtor <code>Int16Array</code> para usar a operação <code>ToIndex</code> e permitir construtores sem argumentos.</td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility">Compatibilidade com navegadores</h2>
<p>{{Compat("javascript.builtins.Int16Array")}}</p>
<h2 id="Notas_de_compatibilidade">Notas de compatibilidade</h2>
<p>A partir do ECMAScript 2015, os construtores <code>Int16Array</code> precisam ser construídos com um operador {{jsxref("Operators/new", "new")}}. Chamar um construtor <code>Int16Array</code> como uma função sem o <code>new</code>, irá resultar em {{jsxref("TypeError")}}.</p>
<pre class="brush: js example-bad">var dv = Int16Array([1, 2, 3]);
// TypeError: calling a builtin Int16Array constructor
// without new is forbidden</pre>
<pre class="brush: js example-good">var dv = new Int16Array([1, 2, 3]);</pre>
<h2 id="Ver_também">Ver também</h2>
<ul>
<li><a href="/en-US/docs/Web/JavaScript/Typed_arrays" title="en/JavaScript typed arrays">JavaScript typed arrays</a></li>
<li>{{jsxref("ArrayBuffer")}}</li>
<li>{{jsxref("DataView")}}</li>
</ul>
|