1
0
mirror of https://github.com/ruanyf/es6tutorial.git synced 2025-05-25 19:22:21 +00:00

新增string/at

This commit is contained in:
ruanyf 2014-11-23 10:02:30 +08:00
parent d1f9686259
commit a80574fa1f

View File

@ -74,6 +74,28 @@ String.fromCodePoint(0x20BB7)
注意fromCodePoint方法定义在String对象上而codePointAt方法定义在字符串的实例对象上。
## at()
ES5提供String.prototype.charAt方法返回字符串给定位置的字符。该方法不能识别Unicode编号大于0xFFFF的字符。
```javascript
'𠮷'.charAt(0)
// '\uD842'
```
上面代码中charAt方法返回的是UTF-16编码的第一个字节实际上是无法显示的。
ES7提供了at方法可以识别Unicode编号大于0xFFFF的字符返回正确的字符。
```javascript
'𠮷'.at(0)
// '𠮷'
```
## 字符的Unicode表示法
JavaScript允许采用“\uxxxx”形式表示一个字符其中“xxxx”表示字符的Unicode编号。