Javascript 高于 U+FFFF 的字符的文字

Javascript literals for characters higher than U+FFFF

我的 javascript 源代码是严格的 ascii,我想用字符串文字表示 anger symbol。在 javascript 中可以吗?

Java脚本字符串实际上是 UTF-16,因此您可以使用 Unicode 转义符编写代理项对:"\uD83D\uDCA2"(这是 Java 源页面上显示的内容代码,也适用于 JavaScript).

从 ES2015 (ES6) 开始,您还可以将其写成 \u{1F4A2} 而不是计算代理对 (spec).

示例:

使用\uD83D\uDCA2:

document.body.innerHTML =
  "(Of course, this only works if the font has that character.)" +
  "<br>As \uD83D\uDCA2: \uD83D\uDCA2";

使用 \u{1F4A2} (如果您的浏览器支持新的 ES2015 功能):

document.body.innerHTML =
  "(Of course, this only works if the font has that character.)" +
  "<br>As \u{1F4A2}: \u{1F4A2}";


这是一个使用 U+1D44E (\uD835\uDC4E, \u{1D44E}) 的示例,数学中使用的程式化 a,显示在 SO 上对我来说 Linux(而你的 :anger: 表情符号没有,但如果我使用 Windows 8.1 就可以):

使用\uD835\uDC4E:

document.body.innerHTML =
  "(Of course, this only works if your browser has that symbol.)" +
  "<br>As \uD835\uDC4E: \uD835\uDC4E";

使用 \u{1D44E} (如果您的浏览器支持新的 ES2015 功能):

document.body.innerHTML =
  "(Of course, this only works if your browser has that symbol.)" +
  "<br>As \u{1D44E}: \u{1D44E}";