Javascript 简明教程
JavaScript - Unicode
What is Unicode?
Unicode 是一个通用字符集,其中包含来自大多数语言、书写系统等字符的列表。它为每个字符提供一个唯一的数字,而不关注编程语言、平台、操作系统等。此外,还包括标点符号、表情符号、特殊字符等。
Unicode is a universal set of characters that contains a list of characters from the majority of languages, writing systems, etc. It provides a unique number for every character without focusing on programming language, platform, operating system, etc. Furthermore, it also includes punctuation, emojis, special characters, etc.
简而言之,Unicode 集包含唯一数字,每个数字都对应一个唯一字符,无论平台、操作系统等如何,都具有相同的含义。
In short, the Unicode set contains unique numbers, each referring to a unique character, having the same meaning regardless of platform, operating system, etc.
Intuition behind Unicode
在理解 unicode 之前,让我们了解一下它背后的想法。你能回答为什么你能阅读本教程的问题吗?嗯,因为你知道所写字母的含义。读者(您)和作者对英文字母表字母都具有相同的理解;因此,您能够阅读作者所写的内容。
Before understanding unicode, let’s understand the idea behind it. Can you answer the question of why are you able to read this tutorial? Well, because you know the meaning of the letters written. A reader (you) and writer both have the same comprehension of the English alphabetical letters; that’s why you are able to read what the writer has written.
类似地,计算机不理解字母。对于计算机,字母是位序列,每个序列都映射到一个称为 Unicode 的唯一字符。
Similarly, computers don’t understand the letters. For computers, letters are sequences of bits, and each sequence is mapped to a unique character that is called Unicode.
现在,让我们深入了解 Unicode。
Now, let’s understand Unicode in depth.
Unicode in JavaScript
JavaScript 允许开发人员在字符串文字和源代码中使用 Unicode 字符。开发人员需要使用转义符号 (\u) 在 JavaScript 代码中使用 Unicode 字符。
JavaScript allows developers to use the Unicode characters in the string literals and source code. Developers need to use the escape notation (\u) to use the Unicode characters in JavaScript code.
Syntax
用户可以遵循下面的语法在 JavaScript 中使用 Unicode 字符。
Users can follow the syntax below to use the Unicode characters in JavaScript.
const char = '\uxxxx';
在上面的语法中,'\uxxxx' 是一个 Unicode 字符。在此,'xxxx' 表示十六进制字符,‘/u' 表示转义符号。
In the above syntax, '\uxxxx' is a Unicode character. Here, 'xxxx' represents the hexadecimal characters, and ‘/u' represents the escape notation.
Examples
Example: Unicode escape sequence
在下面的示例中,我们使用了 Unicode 转义序列来打印 "hello" 信息。
In the below example, we have used unicode escape sequence to print the "hello" message.
<html>
<body>
<div>Using unicode escape sequence</div>
<div id = "output"> </div>
<script>
let str = '\u0068\u0065\u006c\u006c\u006f'
document.getElementById("output").innerHTML = str;
</script>
</body>
</html>
Using unicode escape sequence
hello
Example: Using unicode characters in variable names
在下面的代码中,我们使用了两个不同的 Unicode 字符作为两个不同的标识符(变量名称)。在输出中,您可以观察到两个标识符的值。
In the code below, we have used the two different Unicode characters as two different identifiers (variable names). In the output, you can observe the value of both identifiers.
<html>
<body>
<div>Using unicode characters in variable names</div>
<div id = "output"> </div>
<script>
// Using the Unicode characters in variable names
let \u0061 = "Hello";
let \u0062 = "World";
document.getElementById("output").innerHTML = a + " " + b;
</script>
</body>
</html>
Using unicode characters in variable names
Hello World
Example: Using the Unicode Characters in String
在该示例中,我们在字符串字面量中使用了 Unicode 字符。输出显示了字符串中间的特殊字符。
In this example, we have used the Unicode characters in the string literals. The output shows the special characters in the middle of the string.
<html>
<body>
<div> Using the Unicode Characters in String </div>
<div id = "output"> </div>
<script>
// Using the Unicode characters in the string
let str = 'Hello \u00D8 \u00F8 World';
document.getElementById("output").innerHTML = str;
</script>
</body>
</html>
Using the Unicode Characters in String
Hello Ø ø World
Example: Using Unicode for non-BMP (Basic Multilingual Plane) characters
在下面的示例中,我们使用了 Unicode 字符(代码点)来显示非 BMP(基本多语言平面)字符。我们已经为一名卫生工作者进行了演示。
In the below example we have used unicode characters (code points) to show a non-BMP (basic multilingual plane) characters. We have demonstrate for a health worker.
<html>
<body>
<div>showing person heath worker using unicode code point</div>
<div id = "output"> </div>
<script>
// Showing emojis using the unicode characters
const smileyFace = '\u{1F9D1}\u200D\u2695\uFE0F';
document.getElementById("output").innerHTML = smileyFace;
</script>
</body>
</html>
showing person heath worker using unicode code point
🧑⚕️
Example: Showing Emojies Using the Unicode Characters
在下面的代码中,我们使用了 Unicode 字符来显示笑脸表情符号。
In the code below, we have used the Unicode character to show the smiley face emoji.
<html>
<body>
<div>Showing Emojies Using the Unicode Characters </div>
<div id = "output"> </div>
<script>
// Showing emojis using the unicode characters
const smileyFace = '\uD83D\uDE0A';
document.getElementById("output").innerHTML = smileyFace;
</script>
</body>
</html>
Showing Emojies Using the Unicode Characters
😊
正如我们所看到的,每个 Unicode 字符都表示一个唯一的字符。在 JavaScript 中,我们可以在标识符、字符串字面量等中使用 Unicode 字符。
As we have seen, each Unicode character represents a unique character. In JavaScript, we can use Unicode characters with identifiers, string literals, etc.