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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
---
title: ArrayBuffer
slug: Web/JavaScript/Reference/Global_Objects/ArrayBuffer
translation_of: Web/JavaScript/Reference/Global_Objects/ArrayBuffer
---
<p>{{JSRef}}</p>
<p><strong><code>ArrayBuffer</code></strong> 物件是一種表示通用、固定大小的原始二進制資料緩衝。想要直接操作一個 <code>ArrayBuffer</code> 物件的內容是不可能的。若要讀寫該緩衝的內容則必須透過視圖,可以選擇建立一個 {{jsxref("DataView")}} 視圖物件或是一個限定其成員為某種型別的 {{jsxref("TypedArray")}} 視圖物件,它們皆能以特定的型別解讀、修改 <code>ArrayBuffer</code>。</p>
<h2 id="語法">語法</h2>
<pre class="syntaxbox">new ArrayBuffer(length)
</pre>
<h3 id="參數">參數</h3>
<dl>
<dt><code>length</code></dt>
<dd>要建立的緩衝陣列大小,以位元組(byte)計算。</dd>
</dl>
<h3 id="回傳值">回傳值</h3>
<p>為一個新建立的指定大小 <code>ArrayBuffer</code> 物件,其內容皆初始化為 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")}} (>= 2 ** 53) or negative.</p>
<h2 id="說明">說明</h2>
<p>The <code>ArrayBuffer</code> constructor creates a new <code>ArrayBuffer</code> of the given length in bytes.</p>
<h3 id="從既有的資料取得_ArrayBuffer">從既有的資料取得 ArrayBuffer</h3>
<ul>
<li><a href="/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="/zh-TW/docs/Web/API/FileReader#readAsArrayBuffer()">從本地端檔案</a></li>
</ul>
<h2 id="屬性">屬性</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="方法">方法</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="/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>
<div class="line" id="file-arraybuffer-transfer-LC6">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>.</div>
</dd>
</dl>
<h2 id="ArrayBuffer_實例"><code>ArrayBuffer</code> 實例</h2>
<p>所有的 <code>ArrayBuffer</code> 物件實例皆繼承自 {{jsxref("ArrayBuffer.prototype")}}.</p>
<h3 id="屬性_2">屬性</h3>
<p>{{page('zh-TW/Web/JavaScript/JavaScript_typed_arrays/ArrayBuffer/prototype','屬性')}}</p>
<h3 id="方法_2">方法</h3>
<p>{{page('/zh-TW/docs/Web/JavaScript/JavaScript_typed_arrays/ArrayBuffer/prototype','方法')}}</p>
<dl>
<dt>{{jsxref("ArrayBuffer.slice()")}} {{non-standard_inline}}</dt>
<dd>Has the same functionality as {{jsxref("ArrayBuffer.prototype.slice()")}}.</dd>
</dl>
<h2 id="範例">範例</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 class="brush: js">var buffer = new ArrayBuffer(8);
var view = new Int32Array(buffer);</pre>
<h2 id="規範">規範</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td>{{SpecName('Typed Array')}}</td>
<td>{{Spec2('Typed Array')}}</td>
<td>Superseded by ECMAScript 6.</td>
</tr>
<tr>
<td>{{SpecName('ES6', '#sec-arraybuffer-constructor', 'ArrayBuffer')}}</td>
<td>{{Spec2('ES6')}}</td>
<td>Initial definition in an 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="瀏覽器相容性">瀏覽器相容性</h2>
<p>{{CompatibilityTable}}</p>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Chrome</th>
<th>Edge</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Basic support</td>
<td>7.0</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatGeckoDesktop("2")}}</td>
<td>10</td>
<td>11.6</td>
<td>5.1</td>
</tr>
<tr>
<td><code>ArrayBuffer()</code> without <code>new</code> throws</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatGeckoDesktop("44")}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
</tr>
<tr>
<td><code>ArrayBuffer.slice()</code> {{non-standard_inline}}</td>
<td>{{CompatNo}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}<br>
{{CompatNo}} {{CompatGeckoDesktop("53")}}</td>
<td>{{CompatNo}}</td>
<td>{{CompatNo}}</td>
<td>{{CompatUnknown}}</td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Android</th>
<th>Chrome for Android</th>
<th>Edge</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Basic support</td>
<td>4.0</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatGeckoMobile("2")}}</td>
<td>10</td>
<td>11.6</td>
<td>4.2</td>
</tr>
<tr>
<td><code>ArrayBuffer()</code> without <code>new</code> throws</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatGeckoMobile("44")}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
</tr>
<tr>
<td><code>ArrayBuffer.slice()</code> {{non-standard_inline}}</td>
<td>{{CompatNo}}</td>
<td>{{CompatNo}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}<br>
{{CompatNo}} {{CompatGeckoMobile("53")}}</td>
<td>{{CompatNo}}</td>
<td>{{CompatNo}}</td>
<td>{{CompatUnknown}}</td>
</tr>
</tbody>
</table>
</div>
<h2 id="相容性備註">相容性備註</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);
// TypeError: calling a builtin ArrayBuffer constructor
// without new is forbidden</pre>
<pre class="brush: js example-good">var dv = new ArrayBuffer(10);</pre>
<h2 id="參見">參見</h2>
<ul>
<li>{{jsxref("TypedArray")}}</li>
<li>{{jsxref("SharedArrayBuffer")}}</li>
</ul>
|