From a80574fa1ffc4302b9d2d10461442bd1d7962aac Mon Sep 17 00:00:00 2001 From: ruanyf Date: Sun, 23 Nov 2014 10:02:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Estring/at?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/string.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/docs/string.md b/docs/string.md index 2f077cb..59b0249 100644 --- a/docs/string.md +++ b/docs/string.md @@ -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编号。