aboutsummaryrefslogtreecommitdiff
path: root/files/pt-br/web/javascript/reference/global_objects/arraybuffer/index.html
blob: 60970d10dfa61ae50fb72575376c51f1e33c88a7 (plain)
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
---
title: ArrayBuffer
slug: Web/JavaScript/Reference/Global_Objects/ArrayBuffer
tags:
  - ArrayBuffer
  - Constructor
  - JavaScript
  - TypedArrays
translation_of: Web/JavaScript/Reference/Global_Objects/ArrayBuffer
---
<p>{{JSRef}}</p>

<p>O objeto <strong><code>ArrayBuffer</code></strong> é um tipo de dado usado para representar um genérico, <span class="short_text" id="result_box" lang="pt"><span class="hps">buffer de</span> <span class="hps">dados binários</span> <span class="hps">de tamanho</span> <span class="hps">fixo</span></span>. Você não pode manipular diretamente os conteúdos de um <code>ArrayBuffer</code>;  em vez disso, você cria um objeto <a href="/en/JavaScript_typed_arrays/ArrayBufferView" title="en/JavaScript typed arrays/ArrayBufferView"><code>ArrayBufferView</code></a> que representa o buffer em um formato específico, e usa para ler e escrever os conteúdos do buffer.</p>

<p>{{EmbedInteractiveExample("pages/js/arraybuffer-constructor.html")}}</p>

<div class="hidden">
<p>The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> and send us a pull request.</p>
</div>

<h2 id="Syntax">Syntax</h2>

<pre>new ArrayBuffer(length)
</pre>

<h3 id="Parameters">Parameters</h3>

<dl>
 <dt><code>length</code></dt>
 <dd>The size, in bytes, of the array buffer to create.</dd>
</dl>

<h3 id="Return_value">Return value</h3>

<p>A new <code>ArrayBuffer</code> object of the specified size. Its contents are initialized to 0.</p>

<h3 id="Exceptions">Exceptions</h3>

<p>A {{jsxref("RangeError")}} is thrown if the <code>length</code> is larger than {{jsxref("Number.MAX_SAFE_INTEGER")}} (&gt;= 2 ** 53) or negative.</p>

<h2 id="Description">Description</h2>

<p>The <code>ArrayBuffer</code> constructor creates a new <code>ArrayBuffer</code> of the given length in bytes.</p>

<h3 id="Getting_an_array_buffer_from_existing_data">Getting an array buffer from existing data</h3>

<ul>
 <li><a href="https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#Appendix.3A_Decode_a_Base64_string_to_Uint8Array_or_ArrayBuffer">From a Base64 string</a></li>
 <li><a href="https://developer.mozilla.org/en-US/docs/Web/API/FileReader#readAsArrayBuffer()">From a local file</a></li>
</ul>

<h2 id="Properties">Properties</h2>

<dl>
 <dt><code>ArrayBuffer.length</code></dt>
 <dd>The <code>ArrayBuffer</code> constructor's length property whose value is 1.</dd>
 <dt>{{jsxref("ArrayBuffer.@@species", "get ArrayBuffer[@@species]")}}</dt>
 <dd>The constructor function that is used to create derived objects.</dd>
 <dt>{{jsxref("ArrayBuffer.prototype")}}</dt>
 <dd>Allows the addition of properties to all <code>ArrayBuffer</code> objects.</dd>
</dl>

<h2 id="Methods">Methods</h2>

<dl>
 <dt>{{jsxref("ArrayBuffer.isView", "ArrayBuffer.isView(arg)")}}</dt>
 <dd>Returns <code>true</code> if <code>arg</code> is one of the ArrayBuffer views, such as <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray">typed array objects</a> or a {{jsxref("DataView")}}. Returns <code>false</code> otherwise.</dd>
 <dt>{{jsxref("ArrayBuffer.transfer", "ArrayBuffer.transfer(oldBuffer [, newByteLength])")}} {{experimental_inline}}</dt>
 <dd>
 <p>Returns a new <code>ArrayBuffer</code> whose contents are taken from the <code>oldBuffer</code>'s data and then is either truncated or zero-extended by <code>newByteLength</code>.</p>
 </dd>
</dl>

<h2 id="Instances">Instances</h2>

<p>All <code>ArrayBuffer</code> instances inherit from {{jsxref("ArrayBuffer.prototype")}}.</p>

<h3 id="Properties_2">Properties</h3>

<p>{{page('en-US/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/prototype','Properties')}}</p>

<h3 id="Methods_2">Methods</h3>

<p>{{page('en-US/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/prototype','Methods')}}</p>

<dl>
 <dt>{{jsxref("ArrayBuffer.slice()")}} {{non-standard_inline}}</dt>
 <dd>Has the same functionality as {{jsxref("ArrayBuffer.prototype.slice()")}}.</dd>
</dl>

<h2 id="Exemplo">Exemplo</h2>

<p>In this example, we create a 8-byte buffer with a {{jsxref("Global_Objects/Int32Array", "Int32Array")}} view referring to the buffer:</p>

<pre><code>var buffer = new ArrayBuffer(8);
var view   = new Int32Array(buffer);</code></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 6</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-arraybuffer-constructor', 'ArrayBuffer')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>Definição inicial no ECMA standard. Specified that <code>new</code> is required.</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-arraybuffer-constructor', 'ArrayBuffer')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Compatibilidade com navegadores</h2>

<p>{{Compat("javascript.builtins.ArrayBuffer")}}</p>

<h2 id="Compatibility_notes">Compatibility notes</h2>

<p>Starting with ECMAScript 2015, <code>ArrayBuffer</code> constructors require to be constructed with a {{jsxref("Operators/new", "new")}} operator. Calling an <code>ArrayBuffer</code> constructor as a function without <code>new</code>, will throw a {{jsxref("TypeError")}} from now on.</p>

<pre class="brush: js example-bad">var dv = ArrayBuffer(10);
// TypedError: calling a builtin ArrayBuffer constructor
// without new is forbidden</pre>

<pre class="brush: js example-good">var dv = new ArrayBuffer(10);</pre>

<h2 id="Veja_também">Veja também</h2>

<ul>
 <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays">JavaScript typed arrays</a></li>
 <li>{{jsxref("SharedArrayBuffer")}}</li>
</ul>