简体   繁体   English

javascript标识符名称中的unicode转义序列有什么意义?

[英]What is the point of unicode escape sequences in identifier names in JavaScript?

JavaScript allows for having unicode escape sequences in identifier names... for example: JavaScript允许在标识符名称中包含Unicode转义序列...例如:

var \u0160imeVidas = "blah";

The above variable starts with the (croatian) letter Š, so that the complete name of the variable is "ŠimeVidas". 上面的变量以(克罗地亚语)字母Š开头,因此变量的全名是“ŠimeVidas”。 Now, this is neat, but what's the point? 现在,这很干净,但是有什么意义呢? Is there any scenario where this feature may be of any use? 在任何情况下此功能可能有用吗?

Let's say you've got a library stored in UTF-8 and that the author choose to use non-ASCII characters in the APIs. 假设您已经在UTF-8中存储了一个库,并且作者选择在API中使用非ASCII字符。 Then, let's say that—for some reason—you need to access this library from a file stored in ASCII. 然后,由于某种原因,我们需要从ASCII存储的文件中访问该库。 Allowing the Unicode escape sequences in identifiers allows you to do that. 允许在标识符中使用Unicode转义序列可以使您做到这一点。 There could be other such scenarios, but that's one example. 可能还有其他这样的场景,但这只是一个例子。

The only use I can think of for using the unicode escape sequence when declaring variables is for obfuscation. 我可以想到的唯一的使用是在声明变量时使用Unicode转义序列是为了混淆。 You can, of course, type the following for the same variable: 您当然可以为相同的变量键入以下内容:

var ŠimeVidas = "blah";

Now, if you were to refer to this variable with a random character in the string replaced with the unicode escape sequence, it would be much more difficult to search for and find those references. 现在,如果您要用字符串中的随机字符(用unicode转义序列替换)来引用此变量,则搜索和查找这些引用会更加困难。 Of course, like most other obfuscation technique it would be easily reversible. 当然,像大多数其他混淆技术一样,它很容易逆转。

If you wanted to use a unicode character that isn't mapped to an Alt +Num combination, it could save time on searching for the key code in charmap (or your OS' equivalent). 如果您要使用未映射到Alt + Num组合的unicode字符,则可以节省搜索charmap(或您的操作系统的等效键)中的键代码的时间。 Not great for saving bytes, though. 但是,这对于节省字节不是很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM