javascript - 默认的 JavaScript 字符编码是什么?

标签 javascript html character-encoding

在用 JavaScript 编写加密方法时,我开始想知道我的字符串使用的是什么字符编码,以及为什么。

什么决定了 JavaScript 中的字符编码?这是一个标准吗?通过浏览器?由HTTP请求的头部决定?在<META>包含它的 HTML 标签?提供页面的服务器?

根据我的经验测试(改变不同的设置,然后在一个足够奇怪的字符上使用 charCodeAt 并查看该值匹配的编码)它似乎总是 UTF-8 或 UTF-16,但我不是当然为什么

经过一番疯狂的谷歌搜索,我似乎无法找到这个简单问题的最终答案。

最佳答案

E262 第 8.4 节:

The String type is the set of all finite ordered sequences of zero or more 16-bit unsigned integer values (“elements”). The String type is generally used to represent textual data in a running ECMAScript program, in which case each element in the String is treated as a code unit value (see Clause 6). Each element is regarded as occupying a position within the sequence. These positions are indexed with nonnegative integers. The first element (if any) is at position 0, the next element (if any) at position 1, and so on. The length of a String is the number of elements (i.e., 16-bit values) within it. The empty String has length zero and therefore contains no elements.

When a String contains actual textual data, each element is considered to be a single UTF-16 code unit. Whether or not this is the actual storage format of a String, the characters within a String are numbered by their initial code unit element position as though they were represented using UTF-16. All operations on Strings (except as otherwise stated) treat them as sequences of undifferentiated 16-bit unsigned integers; they do not ensure the resulting String is in normalised form, nor do they ensure language-sensitive results.

这种措辞有点狡猾;这似乎意味着所有重要的事情都将字符串视为每个字符都是 UTF-16 字符,但同时没有任何东西可以确保它全部有效。

需要明确的是,意图 是字符串由 UTF-16 代码点组成。在 ES2015 ,“字符串值”的定义包括这个注释:

A String value is a member of the String type. Each integer value in the sequence usually represents a single 16-bit unit of UTF-16 text. However, ECMAScript does not place any restrictions or requirements on the values except that they must be 16-bit unsigned integers.

所以一个字符串仍然是一个字符串,即使它包含的值不能作为正确的 Unicode 字符工作。

关于javascript - 默认的 JavaScript 字符编码是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11141136/

相关文章:

javascript - 抛出错误 ('msg' ) vs 抛出新错误 ('msg' )

javascript - Selenium Webdriver 执行由于 javascript 等待条件而卡住

php - 获取没有标题/编码的外部网页的html源

javascript - 您如何读取 URL 中带有 "&"符号的哈希值?

delphi - RAR 文件中的非 ASCII 文件名是如何编码的?

PHP:如何对 U+FFFD 进行编码以进行替换?

javascript 对象值到字符串的转换

javascript - 在函数上调用函数 - Jquery

javascript - 在 html 属性中指定一个 javascript 方法?

html - 如何在第二行后添加一条水平线?