天天看點

Javascript 資料結構Javascript 資料結構

<a target="_blank" href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Data_structures#toc">在本文章中</a>

<a target="_blank" href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Data_structures#.E5.8E.9F.E5.A7.8B.E5.80.BC">原始值</a>

<a target="_blank" href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Data_structures#.E5.AD.97.E7.AC.A6.E4.B8.B2">字元串</a>

<a target="_blank" href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Data_structures#.E5.AF.B9.E8.B1.A1">對象</a>

<a target="_blank" href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Data_structures#Arrays">Arrays</a>

<a target="_blank" href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Data_structures#Dates">Dates</a>

<a target="_blank" href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Data_structures#WeakMaps.2C_Maps.2C_Sets">WeakMaps, Maps, Sets</a>

<a target="_blank" href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Data_structures#TypedArrays">TypedArrays</a>

<a target="_blank" href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Data_structures#See_also">See also</a>

程式設計語言都具有内建的資料結構,但各種程式設計語言的資料結構常有不同之處。 本文嘗試展現 JavaScript 語言中内建的資料結構及其屬性,它們可以用來建構其他的資料結構, 同時盡可能的描述 JavaScript 資料結構與其他語言的不同之處。

ECMAScript 标準定義了 6 種資料類型:

Number

String

Boolean

Null

Undefined

Object

在下面的章節, 我們将看到這些資料類型如何被用于表現資料以及組合實作更複雜的資料結構。

在這些類型裡面,隻有4種類型被定義成常量<code>:true,false</code>,<code>null</code>,和<code>undefined</code>。 因為它們是常量,是以不能使用它們來呈現豐富的資料(或資料結構)。

依據 ECMAScript 标準,隻有一種數字類型:它是一個基于IEEE 754标準的雙精度64位二進制格式的值。是以它并沒有為整數給出一種特定的類型。 除了能夠表示浮點型的數值之外,還有一些帶符号的值<code>:+Infinity,-Infinity</code>,和 <code>NaN</code> (非數值)。

盡管一個數字常常僅代表它本身的值,但JavaScript提供了一些位操作符。 這些位操作符和一個單一數字通過位操作可以用來表現一些布爾值。但這通常被認為是一個不好的做法,因為 JavaScript 提供了其他的手段來表示一組布爾值(如一個布爾值數組或一個布爾值配置設定給命名屬性的對象)。 在一些非常受限的情況下,可能需要用到這些技術,比如試圖應付本地存儲的存儲限制orin extreme cases when each bit over the network counts. This technique should only be considered when it is the last thing that can be done to optimize size.

不像 C 語言, JavaScript 字元串是不可更改的。這意味着字元串一旦被建立,就不能被修改。無論如何,它仍然可以通過新建立一個基于操作原字元串的新字元串(這麼繞口),例如:通過String.substring, String.substr, String.slice, String.concat 等操作來修改原字元串時。

It can be tempting to use strings to represent complex data. They have a couple of nice properties:

It's easy to build complex strings with concatenation.

Strings are easy to debug (what you see printed is always what is in the string).

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 becomes a maintenance burden which does not exist when the right tool for the right job is used.

It is recommended to use strings for textual data and symbolic data, but to parse strings and use the right abstraction for what it is supposed to represent otherwise.

Functions are regular objects with the additional capability of being callable.

Non-standard. Likely to be standardized as part of ECMAScript 6.

These data structures take object references as keys. A Set represents a set of objects, while WeakMaps and Maps associates 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.

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.

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

<a target="_blank" href="https://github.com/nzakas/computer-science-in-javascript/">Nicholas Zakas collection of common data structure and common algorithms in JavaScript.</a>

繼續閱讀