20200417

@xiaojingzhao

Plan

  • 你不知道的 js 下卷

Notes

Unicode 字符定位 -- charAt

  • 不支持 astral 字符的原子性
var s1 = "abc\u0301d"; // "abćd"
var s2 = "ab\u0107d"; // "abćd"
var s3 = "ab\u{1d49e}d"; // "ab𝒞d"

s1.charAt(2); // "c"
s2.charAt(2); // "ć"
s3.charAt(2); // 乱码

解决方案: API:

codePointAt(...)

例子:

var s1 = "abc\u0301d"; // "abćd"
var s2 = "ab\u0107d"; // "abćd"
var s3 = "ab\u{1d49e}d"; // "ab𝒞d"

s1.normalize().codePointAt(2).toString(16); // 107
s2.normalize().codePointAt(2).toString(16); // 107
s3.normalize().codePointAt(2).toString(16); // 1d49e

String.fromCodePoint(0x1d49e); // 𝒞

Unicode 标识符名

  • Unicode 也可用作标识符名(变量、属性等)
var Ω = 42; // 等价于 var \u03A9 = 42

支持点码转义

var 𫐀 = 42;

More