aboutsummaryrefslogtreecommitdiff
path: root/files/zh-tw/web/javascript/reference/global_objects/dataview/index.html
blob: d7cf6d6ed49237ccc8740fe915e6b4d7dca77f30 (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
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
---
title: DataView
slug: Web/JavaScript/Reference/Global_Objects/DataView
translation_of: Web/JavaScript/Reference/Global_Objects/DataView
---
<div>{{JSRef}}</div>

<p><strong><code>DataView</code></strong> 視圖提供了一個底層介面來讀寫 {{jsxref("ArrayBuffer")}} 中的二進位資料。<code>DataView</code> 能用多種不同的型別對 <code>ArrayBuffer</code> 進行修改、解讀,且可自訂資料的位元組順序而不受系統平台限制。<code>DataView</code> 物件僅為視圖,並不會存放資料,所有的資料皆實際儲存於 <code>ArrayBuffer</code> 物件當中。</p>

<h2 id="語法">語法</h2>

<pre class="syntaxbox">new DataView(buffer [, byteOffset [, byteLength]])</pre>

<h3 id="參數">參數</h3>

<dl>
 <dt><code>buffer</code></dt>
 <dd>要給DataView物件操作的資料容器並且不能為null</dd>
 <dt><code>byteOffset</code> {{optional_inline}}</dt>
 <dd>The offset, in bytes, to the first byte in the specified buffer for the new view to reference. If not specified, the view of the buffer will start with the first byte.</dd>
 <dt><code>byteLength</code> {{optional_inline}}</dt>
 <dd>The number of elements in the byte array. If unspecified, length of the view will match the buffer's length.</dd>
</dl>

<h3 id="回傳值">回傳值</h3>

<p>A new <code>DataView</code> object representing the specified data buffer.</p>

<h3 id="Errors_thrown">Errors thrown</h3>

<dl>
 <dt><code>{{jsxref("RangeError")}}</code></dt>
 <dd>Thrown if the <code>byteOffset</code> and <code>byteLength</code> result in the specified view extending past the end of the buffer.</dd>
</dl>

<h2 id="描述">描述</h2>

<h3 id="位元組順序">位元組順序</h3>

<p>Multi-byte number formats are represented in memory differently depending on machine architecture, see {{Glossary("Endianness")}} for an explanation. DataView accessors provide explicit control of how data will be accessed irrespective of the platform architecture's endianness.</p>

<pre class="brush: js">var littleEndian = (function() {
  var buffer = new ArrayBuffer(2);
  new DataView(buffer).setInt16(0, 256, true /* littleEndian */);
  // Int16Array uses the platform's endianness.
  return new Int16Array(buffer)[0] === 256;
})();
console.log(littleEndian); // true or false
</pre>

<h2 id="屬性">屬性</h2>

<dl>
 <dt>DataView.length</dt>
 <dd>The <code>DataView</code> constructor's length property whose value is 3.</dd>
 <dt>{{jsxref("DataView.prototype")}}</dt>
 <dd>Allows the addition of properties to all <code>DataView</code> objects.</dd>
</dl>

<h2 id="DataView_實例"><code>DataView</code> 實例</h2>

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

<h3 id="屬性_2">屬性</h3>

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

<h3 id="方法">方法</h3>

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

<h2 id="範例">範例</h2>

<pre class="brush: js">var buffer = new ArrayBuffer(16);
var dv = new DataView(buffer, 0);

dv.setInt16(1, 42);
dv.getInt16(1); //42
</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-dataview-constructor', 'DataView')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>Initial definition in an ECMA standard</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-dataview-constructor', 'DataView')}}</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>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>9.0</td>
   <td>{{CompatGeckoDesktop("15.0")}}</td>
   <td>10</td>
   <td>12.1</td>
   <td>5.1</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>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>{{CompatGeckoMobile("15")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>12.0</td>
   <td>4.2</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="Firefox-specific_notes">Firefox-specific notes</h2>

<p>Starting with Gecko / SpiderMonkey 40 {{geckoRelease(40)}}, <code>DataView</code> requires to be constructed with a {{jsxref("Operators/new", "new")}} operator. Calling <code>DataView()</code> as a function without <code>new</code>, will throw a {{jsxref("TypeError")}} from now on.</p>

<pre class="brush: js example-bad">var dv = DataView(buffer, 0);
// TypeError: calling a builtin DataView constructor without new is forbidden</pre>

<pre class="brush: js example-good">var dv = new DataView(buffer, 0);</pre>

<h2 id="參見">參見</h2>

<ul>
 <li><a class="link-https" href="https://github.com/jDataView/jDataView">jDataView</a>: JavaScript library that polyfills and extends the <code>DataView</code> API to all browsers and Node.js.</li>
</ul>