Skip to content

Commit a80574f

Browse files
committed
新增string/at
1 parent d1f9686 commit a80574f

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

docs/string.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,28 @@ String.fromCodePoint(0x20BB7)
7474

7575
注意,fromCodePoint方法定义在String对象上,而codePointAt方法定义在字符串的实例对象上。
7676

77+
## at()
78+
79+
ES5提供String.prototype.charAt方法,返回字符串给定位置的字符。该方法不能识别Unicode编号大于0xFFFF的字符。
80+
81+
```javascript
82+
83+
'𠮷'.charAt(0)
84+
// '\uD842'
85+
86+
```
87+
88+
上面代码中,charAt方法返回的是UTF-16编码的第一个字节,实际上是无法显示的。
89+
90+
ES7提供了at方法,可以识别Unicode编号大于0xFFFF的字符,返回正确的字符。
91+
92+
```javascript
93+
94+
'𠮷'.at(0)
95+
// '𠮷'
96+
97+
```
98+
7799
## 字符的Unicode表示法
78100

79101
JavaScript允许采用“\uxxxx”形式表示一个字符,其中“xxxx”表示字符的Unicode编号。

0 commit comments

Comments
 (0)