JavaScript Standard built-in objects @ Tue, Feb 28, 2023 3:08 PM
JavaScript Standard built-in objects @ Tue, Feb 28, 2023 3:26 PM
Expand 35 lines ...
36

36

37
(obj.go || obj.stop)(); // (4) dom 环境是 window,node 环境 global
37
(obj.go || obj.stop)(); // (4) dom 环境是 window,node 环境 global
38
```
38
```
39
+

40
+

41
+
## String
42
+

43
+
关于 [surrogate pair](https://en.wikipedia.org/wiki/UTF-16#:~:text=units%20called%20a-,surrogate%20pair,-%2C%20by%20the%20following),比如:😊。
44
+

45
+
```js
46
+
// charCodeAt 不会考虑 surrogate pair,所以返回了 𝒳 前半部分的编码:
47
+

48
+
alert( '𝒳'.charCodeAt(0).toString(16) ); // d835
49
+

50
+
// codePointAt 可以正确处理 surrogate pair
51
+
alert( '𝒳'.codePointAt(0).toString(16) ); // 1d4b3
52
+
```
53
+

54
+
[Unicode Decomposition](https://www.compart.com/en/unicode/U+1E61#:~:text=1E60\)%20%5B1%5D-,Decomposition,-%3A), 变音符号
55
+

56
+
```js
57
+
let s1 = 'S\u0307\u0323'; // Ṩ, S + 上方点符号 + 下方点符号
58
+
let s2 = 'S\u0323\u0307'; // Ṩ, S + 下方点符号 + 上方点符号
59
+

60
+
alert( `s1: ${s1}, s2: ${s2}` );
61
+

62
+
alert( s1 == s2 ); // 尽管这两个字符在我们看来是相通的,但结果却是 false
63
+

64
+
// 使用 normalize 方法来处理这些
65
+

66
+
alert( "S\u0307\u0323".normalize().length ); // 1
67
+

68
+
alert( "S\u0307\u0323".normalize() == "\u1e68" ); // true
69
+
```