1
0
mirror of https://github.com/ruanyf/es6tutorial.git synced 2025-05-24 18:32:22 +00:00

修改string/codePointAt

This commit is contained in:
ruanyf 2014-09-08 23:56:59 +08:00
parent 08aa66f021
commit eea5149133
2 changed files with 6 additions and 3 deletions

View File

@ -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

View File

@ -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方法是测试一个字符由两个字节还是由四个字节组成的最简单方法。