问题描述
我已经完成了一些Google搜索,但是我得到了与编码字符串或文件相关的结果。
I’ve done some Google searches, but I get results related to encoding strings or files.
我可以写我的节点。用UTF-8编写的JavaScript源代码?我可以在注释,字符串或变量名中使用非ASCII字符吗?
Can I write my Node.js JavaScript source code in UTF-8? Can I use non-ASCII characters in comments, strings, or as variable names?
ECMA-262似乎,但Node.js不会运行UTF-16编码的 .js
文件。然而,它将运行UTF-8源并正确解释非ASCII字符。
ECMA-262 seems to require UTF-16 encoding, but Node.js won’t run a UTF-16 encoded .js
file. It will, however run UTF-8 source and correctly interpret non-ASCII characters.
这是设计还是意外?是否指定了支持UTF-8源代码的地方?
So is this by design or by "accident"? Is it specified somewhere that UTF-8 source code is supported?
推荐答案
我找不到说Node处理文件的文档以UTF-8编码,但似乎是通过实验方式:
I can't find documentation that says that Node treats files as encoded in UTF-8, but it seems that way experimentally:
/* Check in your editor that this Javascript file was saved in UTF-8 */
var nonEscaped = "Планета_Зямля";
var escaped = "\u041f\u043b\u0430\u043d\u0435\u0442\u0430\u005f\u0417\u044f\u043c\u043b\u044f";
if (nonEscaped === escaped) {
console.log("They match");
}
以上示例打印匹配
。
请注意,UTF-8支持非BMP代码点(U + 10000及以上),但Javascript在这种情况下有并发症,它会自动将它们转换为代理对。这是该语言的一部分:
Note that UTF-8 supports non-BMP code points (U+10000 and onwards), but Javascript has complications in that case, it automatically converts them to surrogate pairs. This is part of the language:
这篇关于Node.js源代码需要什么编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!