From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/zh-cn/web/api/domstring/binary/index.html | 23 +++++++++++ files/zh-cn/web/api/domstring/index.html | 52 +++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 files/zh-cn/web/api/domstring/binary/index.html create mode 100644 files/zh-cn/web/api/domstring/index.html (limited to 'files/zh-cn/web/api/domstring') diff --git a/files/zh-cn/web/api/domstring/binary/index.html b/files/zh-cn/web/api/domstring/binary/index.html new file mode 100644 index 0000000000..4eab745a82 --- /dev/null +++ b/files/zh-cn/web/api/domstring/binary/index.html @@ -0,0 +1,23 @@ +--- +title: Binary strings +slug: Web/API/DOMString/Binary +translation_of: Web/API/DOMString/Binary +--- +

{{jsxref("String", "JavaScript strings")}} 是 UTF-16 编码的字符串。它的一个子集是ASCII 字符集(i.e., 字符的码点不会超过 127)。比如,  "Hello world!"这个字符串属于 ASCII 子集, 而 "ÀÈÌÒÙ" 不属于ASCII。binary string 是JS字符集的另外一个子集,它类似于 ASCII 字符集,但是字符的码点(charCode)不再限制到 127, 它包含了255 以内的字符。 binary string设计的目的不是用于代表字符, 而是代表二进制数据。由 binary string 代表的二进制数据大小是原始数据的两倍,然而这对于最终用户是不可见的, 因为JavaScript strings 的长度是以2字节为单位进行计算的。

+ +

Binary strings 不是JavaScript 语言的设计。 然而至少有一个native 函数以它作为输入 ,比如{{domxref("WindowBase64.btoa","btoa()")}}: 给这个函数传入charcode 大于255 的字符串会抛出一个 Character Out Of Range 的错误。

+ +

引入Binary strings来代表uint8 数字的原因是由于 web 应用变得越来越强大(比如操作音频和视频, 使用WebSockets获取二进制数据, 等等)很明显,有一种可以让JavaScript可以简单而快速地操作二进制数据的api 将会提供很大的帮助。

+ +

在以前, 操作二进制数据必须通过对字符串的操作来模拟。使用 charCodeAt() 方法从Binary strings读取数据. 然而这么做又慢又容易出错, 因为需要多次转换(尤其是当数据不是真正的 byte-format data,而是 32-bit 整数或者浮点数)。

+ +

JavaScript typed arrays 提供了一个操作 二进制数据更加高效的方法。StringView 这个非 native的构造函数是构建在 typed arrays 上的为字符串提供了一个 C-like的接口。

+ +

参见

+ + diff --git a/files/zh-cn/web/api/domstring/index.html b/files/zh-cn/web/api/domstring/index.html new file mode 100644 index 0000000000..94ad3ad373 --- /dev/null +++ b/files/zh-cn/web/api/domstring/index.html @@ -0,0 +1,52 @@ +--- +title: DOMString +slug: Web/API/DOMString +tags: + - DOMString +translation_of: Web/API/DOMString +--- +

{{APIRef("DOM")}}

+ +

DOMString 是一个UTF-16字符串。由于JavaScript已经使用了这样的字符串,所以DOMString 直接映射到 一个{{jsxref("String")}}。

+ +

null传递给接受DOMString的方法或参数时通常会把其stringifies为“null”。

+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('WebIDL', '#DOMString', 'DOMString')}}{{Spec2('WebIDL')}}Rephrasing of the definition to remove weird edge cases.
{{SpecName('DOM3 Core', 'core.html#DOMString', 'DOMString')}}{{Spec2('DOM3 Core')}}No change from {{SpecName('DOM2 Core')}}
{{SpecName('DOM2 Core', 'core.html#ID-C74D1578', 'DOMString')}}{{Spec2('DOM2 Core')}}No change from {{SpecName('DOM1')}}
{{SpecName('DOM1', 'level-one-core.html#ID-C74D1578', 'DOMString')}}{{Spec2('DOM1')}}Initial definition.
+ +

相关链接

+ + -- cgit v1.2.3-54-g00ecf