mirror of
https://github.com/ruanyf/es6tutorial.git
synced 2025-05-28 21:32:20 +00:00
edit docs/string
This commit is contained in:
parent
46268a659d
commit
37413140d6
@ -37,7 +37,7 @@ codePointAt方法是测试一个字符由两个字节还是由四个字节组成
|
||||
|
||||
```javascript
|
||||
function is32Bit(c) {
|
||||
return c.codePointAt(0) > 0xFFFF;
|
||||
return c.codePointAt(0) > 0xFFFF;
|
||||
}
|
||||
|
||||
is32Bit("𠮷") // true
|
||||
@ -49,10 +49,8 @@ is32Bit("a") // false
|
||||
ES5提供String.fromCharCode方法,用于从码点返回对应字符,但是这个方法不能识别辅助平面的字符(编号大于0xFFFF)。
|
||||
|
||||
```javascript
|
||||
|
||||
String.fromCharCode(0x20BB7)
|
||||
// "ஷ"
|
||||
|
||||
```
|
||||
|
||||
上面代码中,最后返回码点U+0BB7对应的字符,而不是码点U+20BB7对应的字符。
|
||||
@ -60,32 +58,28 @@ String.fromCharCode(0x20BB7)
|
||||
ES6提供了String.fromCodePoint方法,可以识别0xFFFF的字符,弥补了String.fromCharCode方法的不足。在作用上,正好与codePointAt方法相反。
|
||||
|
||||
```javascript
|
||||
|
||||
String.fromCodePoint(0x20BB7)
|
||||
// "𠮷"
|
||||
|
||||
```
|
||||
|
||||
注意,fromCodePoint方法定义在String对象上,而codePointAt方法定义在字符串的实例对象上。
|
||||
|
||||
## String.prototype.at()
|
||||
## at()
|
||||
|
||||
ES5提供String.prototype.charAt方法,返回字符串给定位置的字符。该方法不能识别码点大于0xFFFF的字符。
|
||||
ES5对字符串对象提供charAt方法,返回字符串给定位置的字符。该方法不能识别码点大于0xFFFF的字符。
|
||||
|
||||
```javascript
|
||||
|
||||
'𠮷'.charAt(0)
|
||||
// '\uD842'
|
||||
|
||||
'abc'.charAt(0) // "a"
|
||||
'𠮷'.charAt(0) // "\uD842"
|
||||
```
|
||||
|
||||
上面代码中,charAt方法返回的是UTF-16编码的第一个字节,实际上是无法显示的。
|
||||
|
||||
ES7提供了字符串实例的at方法,可以识别Unicode编号大于0xFFFF的字符,返回正确的字符。
|
||||
ES7提供了字符串实例的at方法,可以识别Unicode编号大于0xFFFF的字符,返回正确的字符。Chrome浏览器已经支持该方法。
|
||||
|
||||
```javascript
|
||||
'𠮷'.at(0)
|
||||
// '𠮷'
|
||||
'abc'.at(0) // "a"
|
||||
'𠮷'.at(0) // "𠮷"
|
||||
```
|
||||
|
||||
## 字符的Unicode表示法
|
||||
|
Loading…
x
Reference in New Issue
Block a user