aboutsummaryrefslogtreecommitdiff
path: root/files/zh-tw/web/javascript/data_structures/index.html
blob: 29f1a048b8dd08b2918aefb058091673db20b31c (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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
---
title: JavaScript 的資料型別與資料結構
slug: Web/JavaScript/Data_structures
translation_of: Web/JavaScript/Data_structures
---
<div>{{jsSidebar("More")}}</div>

<p>程式語言都具有資料結構,但在不同的程式語言之間會有些差異。這裡將列出可以在 JavaScript 中使用的資料結構,以及它們的特性;它們可以用來構成其他的資料結構。如果可以的話,會標示與其他語言不同的地方。</p>

<h2 id="動態型別">動態型別</h2>

<p>JavaScript 是弱型別,也能說是動態的程式語言。這代表你不必特別宣告變數的型別。程式在運作時,型別會自動轉換。這也代表你可以以不同的型別使用同一個變數。</p>

<pre class="brush: js notranslate">var foo = 42;    // foo 目前是數字
var foo = 'bar'; // foo 目前是字串
var foo = true;  // foo 目前是布林值
</pre>

<h2 id="資料型別">資料型別</h2>

<p>最新的 ECMAScript 標準定義了七種資料型別:</p>

<ul>
 <li>有六種資料型別是{{Glossary("Primitive", "原始型別")}}<ul>
   <li>{{Glossary("Boolean")}}</li>
   <li>{{Glossary("Null")}}</li>
   <li>{{Glossary("Undefined")}}</li>
   <li>{{Glossary("Number")}}</li>
   <li>{{Glossary("BigInt")}}</li>
   <li>{{Glossary("String")}}</li>
   <li>{{Glossary("Symbol")}}(於 ECMAScript 6 新定義)</li>
  </ul>
 </li>
 <li>另外還有 {{Glossary("Object")}}</li>
</ul>

<h2 id="原始值">原始值</h2>

<p>除了物件以外的所有值,都是原始定義的值(值意味著不能被改變)。例如與 C 不同的地方,就是字串是不變的。我們引用這些類型的值為 primitive values(原始值)。</p>

<h3 id="布林型別">布林型別</h3>

<p>布林(Boolean)代表了有兩個值的邏輯實體:<code>true</code><code>false</code></p>

<h3 id="Null_型別">Null 型別</h3>

<p>Null 型別只有一個值:<code>null</code>。請參見 {{jsxref("null")}}{{Glossary("Null")}}</p>

<h3 id="未定義型別">未定義型別</h3>

<p>一個沒有被定義的變數有 <code>undefined</code> 值。請參見 {{jsxref("undefined")}}{{Glossary("Undefined")}}</p>

<h3 id="數字型別">數字型別</h3>

<p>根據 ECMAScript 標準,數字型別只有一種:<a href="https://zh.wikipedia.org/wiki/%E9%9B%99%E7%B2%BE%E5%BA%A6%E6%B5%AE%E9%BB%9E%E6%95%B8">雙精度 64 位元二進制格式 IEEE 754 值</a>(在 -(2<sup>53</sup> -1) and 2<sup>53</sup> -1 之間的數字)。<strong>而整數並沒有指定的型別</strong>。數字除了能代表浮點數以外,還有三個符號值:<code>+Infinity</code><code>-Infinity</code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN"><code>NaN</code></a>(not-a-number,非數字)。</p>

<p>要檢查在 <code>+/-Infinity</code> 內可用的最大值或最小值,可以用 {{jsxref("Number.MAX_VALUE")}}{{jsxref("Number.MIN_VALUE")}} 常數。從 ECMAScript 6 開始,也可以透過 {{jsxref("Number.isSafeInteger()")}}{{jsxref("Number.MAX_SAFE_INTEGER")}}{{jsxref("Number.MIN_SAFE_INTEGER")}} 檢查某數字是否為雙精度浮點值之間。Beyond this range, integers in JavaScript are not safe anymore and will be a double-precision floating point approximation of the value.</p>

<p>The number type has only one integer that has two representations: 0 is represented as -0 and +0. ("0" is an alias for +0). In the praxis, this has almost no impact. For example <code>+0 === -0</code> is <code>true</code>. However, you are able to notice this when you divide by zero:</p>

<pre class="brush: js notranslate">&gt; 42 / +0
Infinity
&gt; 42 / -0
-Infinity
</pre>

<p>Although a number often represents only its value, JavaScript provides <a href="/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators" title="en/JavaScript/Reference/Operators/Bitwise_Operators">some binary operators</a>. These can be used to represent several Boolean values within a single number using <a class="external" href="http://en.wikipedia.org/wiki/Mask_%28computing%29">bit masking</a>. However, this is usually considered a bad practice, since JavaScript offers other means to represent a set of Booleans (like an array of Booleans or an object with Boolean values assigned to named properties). Bit masking also tends to make code more difficult to read, understand, and maintain. It may be necessary to use such techniques in very constrained environments, like when trying to cope with the storage limitation of local storage or in extreme cases when each bit over the network counts. This technique should only be considered when it is the last measure that can be taken to optimize size.</p>

<h3 id="字串型別">字串型別</h3>

<p>JavaScript 的 {{jsxref("Global_Objects/String", "字串")}}型別用來代表文字資料。它是一組 16 位的未宣告「元素」值。每個字串的元素,在字串內皆佔有一位。第一個元素位於索引的第 0 位,下一個元素位於第 1 位,並依此類推。字串的長度,是指該字串有多少元素。</p>

<p>與 C 這類的語言不同,JavaScript 字串是不變的,意思是說當字串被創造出來以後,你不可能修改它。不過,可以基於操作原來的字串,來產生新的字串。例如:</p>

<ul>
 <li>透過挑選單個字母或使用 {{jsxref("String.substr()")}} 的原始子字串。</li>
 <li>使用級聯運算符(<code>+</code>)或 {{jsxref("String.concat()")}} 連接兩個字符串。</li>
</ul>

<h4 id="Beware_of_stringly-typing_your_code!">Beware of "stringly-typing" your code!</h4>

<p>It can be tempting to use strings to represent complex data. Doing this comes with short-term benefits:</p>

<ul>
 <li>It is easy to build complex strings with concatenation.</li>
 <li>Strings are easy to debug (what you see printed is always what is in the string).</li>
 <li>Strings are the common denominator of a lot of APIs (<a href="/en-US/docs/Web/API/HTMLInputElement" title="HTMLInputElement">input fields</a>, <a href="/en-US/docs/Storage" title="Storage">local storage</a> values, {{ domxref("XMLHttpRequest") }} responses when using <code>responseText</code>, etc.) and it can be tempting to only work with strings.</li>
</ul>

<p>With conventions, it is possible to represent any data structure in a string. This does not make it a good idea. For instance, with a separator, one could emulate a list (while a JavaScript array would be more suitable). Unfortunately, when the separator is used in one of the "list" elements, then, the list is broken. An escape character can be chosen, etc. All of this requires conventions and creates an unnecessary maintenance burden.</p>

<p>Use strings for textual data. When representing complex data, parse strings and use the appropriate abstraction.</p>

<h3 id="Symbol_type">Symbol type</h3>

<p>Symbols are new to JavaScript in ECMAScript Edition 6. A Symbol is a <strong>unique</strong> and <strong>immutable</strong> primitive value and may be used as the key of an Object property (see below). In some programming languages, Symbols are called atoms. You can also compare them to named enumerations (enum) in C. For more details see {{Glossary("Symbol")}} and the {{jsxref("Symbol")}} object wrapper in JavaScript.</p>

<h2 id="Objects">Objects</h2>

<p>以資訊科學而言,物件是個能透過{{Glossary("Identifier", "identifier")}}參照的有數值記憶體。</p>

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

<p>In JavaScript, objects can be seen as a collection of properties. With the <a href="/en-US/docs/Web/JavaScript/Guide/Values,_variables,_and_literals#Object_literals">object literal syntax</a>, a limited set of properties are initialized; then properties can be added and removed. Property values can be values of any type, including other objects, which enables building complex data structures. Properties are identified using key values. A key value is either a String or a Symbol value.</p>

<p>There are two types of object properties which have certain attributes: The data property and the accessor property.</p>

<h4 id="Data_property">Data property</h4>

<p>Associates a key with a value and has the following attributes:</p>

<table class="standard-table">
 <caption>Attributes of a data property</caption>
 <tbody>
  <tr>
   <th>Attribute</th>
   <th>Type</th>
   <th>Description</th>
   <th>Default value</th>
  </tr>
  <tr>
   <td>[[Value]]</td>
   <td>Any JavaScript type</td>
   <td>The value retrieved by a get access of the property.</td>
   <td>undefined</td>
  </tr>
  <tr>
   <td>[[Writable]]</td>
   <td>Boolean</td>
   <td>If <code>false</code>, the property's [[Value]] can't be changed.</td>
   <td>false</td>
  </tr>
  <tr>
   <td>[[Enumerable]]</td>
   <td>Boolean</td>
   <td>If <code>true</code>, the property will be enumerated in <a href="/en-US/docs/Web/JavaScript/Reference/Statements/for...in">for...in</a> loops. See also <a href="/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties">Enumerability and ownership of properties</a></td>
   <td>false</td>
  </tr>
  <tr>
   <td>[[Configurable]]</td>
   <td>Boolean</td>
   <td>If <code>false</code>, the property can't be deleted and attributes other than [[Value]] and [[Writable]] can't be changed.</td>
   <td>false</td>
  </tr>
 </tbody>
</table>

<table class="standard-table">
 <caption>Obsolete attributes (as of ECMAScript 3, renamed in ECMAScript 5)</caption>
 <tbody>
  <tr>
   <th>Attribute</th>
   <th>Type</th>
   <th>Description</th>
  </tr>
  <tr>
   <td>Read-only</td>
   <td>Boolean</td>
   <td>Reversed state of the ES5 [[Writable]] attribute.</td>
  </tr>
  <tr>
   <td>DontEnum</td>
   <td>Boolean</td>
   <td>Reversed state of the ES5 [[Enumerable]] attribute.</td>
  </tr>
  <tr>
   <td>DontDelete</td>
   <td>Boolean</td>
   <td>Reversed state of the ES5 [[Configurable]] attribute.</td>
  </tr>
 </tbody>
</table>

<h4 id="Accessor_property">Accessor property</h4>

<p>Associates a key with one or two accessor functions (get and set) to retrieve or store a value and has the following attributes:</p>

<table class="standard-table">
 <caption>Attributes of an accessor property</caption>
 <tbody>
  <tr>
   <th>Attribute</th>
   <th>Type</th>
   <th>Description</th>
   <th>Default value</th>
  </tr>
  <tr>
   <td>[[Get]]</td>
   <td>Function object or undefined</td>
   <td>The function is called with an empty argument list and retrieves the property value whenever a get access to the value is performed. See also <a href="/en-US/docs/Web/JavaScript/Reference/Operators/get"><code>get</code></a>.</td>
   <td>undefined</td>
  </tr>
  <tr>
   <td>[[Set]]</td>
   <td>Function object or undefined</td>
   <td>The function is called with an argument that contains the assigned value and is executed whenever a specified property is attempted to be changed. See also <a href="/en-US/docs/Web/JavaScript/Reference/Operators/set"><code>set</code></a>.</td>
   <td>undefined</td>
  </tr>
  <tr>
   <td>[[Enumerable]]</td>
   <td>Boolean</td>
   <td>If <code>true</code>, the property will be enumerated in <a href="/en-US/docs/Web/JavaScript/Reference/Statements/for...in">for...in</a> loops.</td>
   <td>false</td>
  </tr>
  <tr>
   <td>[[Configurable]]</td>
   <td>Boolean</td>
   <td>If <code>false</code>, the property can't be deleted and can't be changed to a data property.</td>
   <td>false</td>
  </tr>
 </tbody>
</table>

<div class="note">
<p><strong>Note: </strong>Attribute is usually used by JavaScript engine, so you can't directly access it(see more about <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty">Object.defineProperty()</a>).That's why the attribute is put in double square brackets instead of single.</p>
</div>

<h3 id="Normal_objects_and_functions">"Normal" objects, and functions</h3>

<p>A JavaScript object is a mapping between keys and values. Keys are strings (or {{jsxref("Symbol")}}s) and values can be anything. This makes objects a natural fit for <a class="external" href="http://en.wikipedia.org/wiki/Hash_table">hashmaps</a>.</p>

<p>Functions are regular objects with the additional capability of being callable.</p>

<h3 id="Dates">Dates</h3>

<p>When representing dates, the best choice is to use the built-in <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date"><code>Date</code> utility</a> in JavaScript.</p>

<h3 id="Indexed_collections_Arrays_and_typed_Arrays">Indexed collections: Arrays and typed Arrays</h3>

<p><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array" title="Array">Arrays</a> are regular objects for which there is a particular relationship between integer-key-ed properties and the 'length' property. Additionally, arrays inherit from <code>Array.prototype</code> which provides to them a handful of convenient methods to manipulate arrays. For example, <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf" title="en/JavaScript/Reference/Global_Objects/Array/indexOf">indexOf</a></code> (searching a value in the array) or <code><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/push" title="en/JavaScript/Reference/Global_Objects/Array/push">push</a></code> (adding an element to the array), etc. This makes Arrays a perfect candidate to represent lists or sets.</p>

<p><a href="/en-US/docs/Web/JavaScript/Typed_arrays">Typed Arrays</a> are new to JavaScript with ECMAScript Edition 6 and present an array-like view of an underlying binary data buffer. The following table helps you to find the equivalent C data types:</p>

<p>{{page("/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray", "TypedArray_objects", "", 0, 3)}}</p>

<h3 id="Keyed_collections_Maps_Sets_WeakMaps_WeakSets">Keyed collections: Maps, Sets, WeakMaps, WeakSets</h3>

<p>These data structures take object references as keys and are introduced in ECMAScript Edition 6. {{jsxref("Set")}} and {{jsxref("WeakSet")}} represent a set of objects, while {{jsxref("Map")}} and {{jsxref("WeakMap")}} associate a value to an object. The difference between Maps and WeakMaps is that in the former, object keys can be enumerated over. This allows garbage collection optimizations in the latter case.</p>

<p>One could implement Maps and Sets in pure ECMAScript 5. However, since objects cannot be compared (in the sense of "less than" for instance), look-up performance would necessarily be linear. Native implementations of them (including WeakMaps) can have look-up performance that is approximately logarithmic to constant time.</p>

<p>Usually, to bind data to a DOM node, one could set properties directly on the object or use <code>data-*</code> attributes. This has the downside that the data is available to any script running in the same context. Maps and WeakMaps make it easy to privately bind data to an object.</p>

<h3 id="Structured_data_JSON">Structured data: JSON</h3>

<p>JSON (JavaScript Object Notation) is a lightweight data-interchange format, derived from JavaScript but used by many programming languages. JSON builds universal data structures. See {{Glossary("JSON")}} and {{jsxref("JSON")}} for more details.</p>

<h3 id="More_objects_in_the_standard_library">More objects in the standard library</h3>

<p>JavaScript has a standard library of built-in objects. Please have a look at the <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects">reference</a> to find out about more objects.</p>

<h2 id="使用_typeof_運算子來判斷型別"><code><font face="x-locale-heading-primary, zillaslab, Palatino, Palatino Linotype, x-locale-heading-secondary, serif">使用 </font>typeof</code> 運算子來判斷型別</h2>

<p><code>typeof</code> 運算子可以幫助你找到你的變數型別,請閱讀〈<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof">reference page</a> 〉來取得更多細節及邊緣案例。</p>

<h2 id="Specifications">Specifications</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('ES1')}}</td>
   <td>{{Spec2('ES1')}}</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-8', 'Types')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td></td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-ecmascript-data-types-and-values', 'ECMAScript Data Types and Values')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td></td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-ecmascript-data-types-and-values', 'ECMAScript Data Types and Values')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td></td>
  </tr>
 </tbody>
</table>

<h2 id="See_also">See also</h2>

<ul>
 <li><a class="link-https" href="https://github.com/nzakas/computer-science-in-javascript/">Nicholas Zakas collection of common data structure and common algorithms in JavaScript.</a></li>
 <li><a href="https://github.com/monmohan/DataStructures_In_Javascript" title="https://github.com/monmohan/DataStructures_In_Javascript">Search Tre(i)es implemented in JavaScript</a></li>
</ul>