mirror of
https://github.com/ruanyf/es6tutorial.git
synced 2025-05-24 18:32:22 +00:00
修改string/codePointAt
This commit is contained in:
parent
08aa66f021
commit
eea5149133
@ -29,6 +29,7 @@
|
|||||||
- Addy Osmani, [Data-binding Revolutions with Object.observe()](http://www.html5rocks.com/en/tutorials/es7/observe/): 介绍Object.observer()的概念
|
- Addy Osmani, [Data-binding Revolutions with Object.observe()](http://www.html5rocks.com/en/tutorials/es7/observe/): 介绍Object.observer()的概念
|
||||||
- Dmitry Soshnikov, [ES6 Notes: Default values of parameters](http://dmitrysoshnikov.com/ecmascript/es6-notes-default-values-of-parameters/): 介绍参数的默认值
|
- Dmitry Soshnikov, [ES6 Notes: Default values of parameters](http://dmitrysoshnikov.com/ecmascript/es6-notes-default-values-of-parameters/): 介绍参数的默认值
|
||||||
- Mozilla Developer Network, [WeakSet](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet):介绍WeakSet数据结构
|
- Mozilla Developer Network, [WeakSet](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet):介绍WeakSet数据结构
|
||||||
|
- Mathias Bynens, [Unicode-aware regular expressions in ES6](https://mathiasbynens.be/notes/es6-unicode-regex): 详细介绍正则表达式的u修饰符
|
||||||
|
|
||||||
## Generator
|
## Generator
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@ s.charCodeAt(1) // 57271
|
|||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
上面代码说明,对于4个字节储存的字符,JavaScript不能正确处理。字符串长度会误判为2,而且charAt方法无法读取字符,charCodeAt方法只能分别返回前两个字节和后两个字节的值。
|
上面代码中,汉字“𠮷”的Unicode编号是0x20BB7,需要4个字节储存。对于这种4个字节的字符,JavaScript不能正确处理,字符串长度会误判为2,而且charAt方法无法读取字符,charCodeAt方法只能分别返回前两个字节和后两个字节的值。
|
||||||
|
|
||||||
ES6提供了codePointAt方法,能够正确处理4个字节储存的字符,返回一个字符的Unicode编号。
|
ES6提供了codePointAt方法,能够正确处理4个字节储存的字符,返回一个字符的Unicode编号。
|
||||||
|
|
||||||
@ -27,13 +27,15 @@ ES6提供了codePointAt方法,能够正确处理4个字节储存的字符,
|
|||||||
var s = "𠮷a";
|
var s = "𠮷a";
|
||||||
|
|
||||||
s.codePointAt(0) // 134071
|
s.codePointAt(0) // 134071
|
||||||
s.codePointAt(1) // 97
|
s.codePointAt(1) // 57271
|
||||||
|
|
||||||
s.charCodeAt(2) // 97
|
s.charCodeAt(2) // 97
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
codePointAt方法的参数,是字符在字符串中的位置(从0开始)。上面代码表明,它会正确返回四字节的UTF-16字符的Unicode编号。对于那些两个字节储存的常规字符,它的返回结果与charCodeAt方法相同。
|
codePointAt方法的参数,是字符在字符串中的位置(从0开始)。上面代码中,JavaScript将“𠮷a”视为三个字符,codePointAt方法在第一个字符上,正确地识别了“𠮷”,返回了它的十进制Unicode编号134071(即十六进制的20BB7)。在第二个字符(即“𠮷”的后两个字节)和第三个字符“a”上,codePointAt方法的结果与charCodeAt方法相同。
|
||||||
|
|
||||||
|
总之,codePointAt方法会正确返回四字节的UTF-16字符的Unicode编号。对于那些两个字节储存的常规字符,它的返回结果与charCodeAt方法相同。
|
||||||
|
|
||||||
codePointAt方法是测试一个字符由两个字节还是由四个字节组成的最简单方法。
|
codePointAt方法是测试一个字符由两个字节还是由四个字节组成的最简单方法。
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user