在Javascript中使用unicode [英] using unicode in Javascript

查看:71
本文介绍了在Javascript中使用unicode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaScript中,我们可以使用下面的代码行(使用Unicode)来显示版权符号:

In JavaScript we can use the below line of code(which uses Unicode) for displaying copyright symbol:

var x = "\u00A9 RPeripherals";

为什么我们不能直接使用 ALT代码(alt + 0169)输入版权符号如下所示:

Why can't we type the copyright symbol directly using ALT code (alt+0169) like below :

var x = "© RPeripherals" ;

这两种方法有什么区别?

What is the difference between these two methods?

推荐答案


为什么我们不能直接使用ALT代码(alt + 0169)直接输入版权符号,如下所示:

Why can't we type the copyright symbol directly using ALT code (alt+0169) like below :

谁这样说?当然可以。只需将代码编辑器配置为对源文件使用UTF-8编码即可。你永远不应该使用任何其他东西......

Who says so? Of course you can. Just configure your code editor to use UTF-8 encoding for source files. You should never use anything else to begin with...


这两种方法有什么区别?

What is the difference between these two methods?

区别在于使用 \uXXXX 方案,您最多传输2个,最多5个传输额外电线上的字节数。如果您需要在源代码中嵌入字符,您的字体无法正确显示,这种拼写可能会有所帮助。例如,我没有使用我用于编程的字体中的繁体中文字符,所以如果我在我的代码编辑器中输入中文字符,我会看到一堆带有Unicode代码点数字的问号或矩形而不是实际的字符。但是在字体中有中文字形的人不会有这个问题。

The difference is that using the \uXXXX scheme you are transmitting at best 2 and at worst 5 extra bytes on the wire. This kind of spelling may help if you need to embed characters in your source code, which your font cannot display properly. For example, I don't have traditional Chinese characters in the font I'm using for programming, so if I type Chinese characters into my code editor, I'll see a bunch of question marks or rectangles with Unicode codepoint digits instead of actual characters. But someone who has Chinese glyphs in the font wouldn't have that problem.

如果我和那个人想要分享我们的源代码,那么另一个人会更好person使用 \uXXXX 方案,因为我可以通过在Unicode表中查找它来验证哪个字符。这就是差别所在。

If me and that person want to share our source code, it would be preferable that the other person uses \uXXXX scheme, as I would be able to verify which character is that by looking it up in the Unicode table. That's about all the difference.

编辑

ECMAScript标准(v 262 / 5.1)具体说明

ECMAScript standard (v 262/5.1) says specifically that


本标准的符合要求的实现应解释符合Unicode标准版本3.0或版本3.0的
字符或
以后和ISO / IEC 10646-1,UCS-2或UTF-16作为采用的
编码形式,实现级别3.如果采用的ISO / IEC 10646-1
子集是除非另有说明,否则推定为BMP
子集,集合300.如果采用的编码形式不是指定的
,则推定为UTF-16编码形式。

A conforming implementation of this Standard shall interpret characters in conformance with the Unicode Standard, Version 3.0 or later and ISO/IEC 10646-1 with either UCS-2 or UTF-16 as the adopted encoding form, implementation level 3. If the adopted ISO/IEC 10646-1 subset is not otherwise specified, it is presumed to be the BMP subset, collection 300. If the adopted encoding form is not otherwise specified, it presumed to be the UTF-16 encoding form.

因此,标准保证字符编码是Unicode,并强制使用UTF-16(这很奇怪,我认为它是UTF-8),但我不认为这是实践中发生的事情......我相信浏览器默认使用UTF-8。也许这在后来的标准中有所改变,但这是最后普遍接受的。

So, the standard guarantees that character encoding is Unicode, and enforces the use of UTF-16 (that's strange, I thought it was UTF-8), but I don't think that this is what happens in practice... I believe that browsers use UTF-8 as default. Perhaps this have changed in the later standards, but this is the one last universally accepted.

这篇关于在Javascript中使用unicode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆