gpt4 book ai didi

javascript - 编辑器是 utf8,javascript 规范需要 utf16

转载 作者:行者123 更新时间:2023-11-30 20:34:50 25 4
gpt4 key购买 nike

ECMAScript 规范说明如下:

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.

来源:http://ecma-international.org/ecma-262/5.1/#sec-8.4

多年来,我一直在使用以 utf8 编码配置的编辑器编写 javascript。为什么我从来没有遇到过字符串问题?我主要使用简单的英语和偶尔的表情符号发送代码。为什么我的代码是 utf8 编码却从未遇到过问题,而规范说您需要 utf16?

最佳答案

您引用了有关字符串类型的部分,该部分是关于 JavaScript 代码使用的字符串的运行时表示形式 - 例如它说明了 charAt 调用的行为方式。

JavaScript 源代码的文本格式和编码是完全不同的事情。您需要阅读 section 6, Source Text , 为了那个原因。不可否认,它说解析器也对 16 位文本代码单元进行操作(但它假定它们是 unicode 规范化的)。它还指出

If an actual source text is encoded in a form other than 16-bit code units it must be processed as if it was first converted to UTF-16.

因此当您发送编码为 UTF-8(这几乎是标准)的 JS 文件并发送适当的 header 时,浏览器将解码 UTF-8 并根据语法规则解析结果。

关于javascript - 编辑器是 utf8,javascript 规范需要 utf16,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49953027/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com