File tree Expand file tree Collapse file tree 13 files changed +248
-12
lines changed
Expand file tree Collapse file tree 13 files changed +248
-12
lines changed Original file line number Diff line number Diff line change 88
99阮老师的那本书确实不错,值得 ES6 初学者去阅读。但是阮老师对这本书的定位是“中级难度”,也就是说书中不会很深入地去剖析各个知识点,而《 Exploring ES6 》这本书就努力在向大家细致地深入地讲解 ES6 的方方面面,这也是我觉得这本书很不错的原因。
1010
11- 此外,《 Exploring ES6 》的作者对 JavaScript 有比较少深入全面的了解 ,也出了一本 ES5 及之前版本的书[ 《 Speaking JavaScript: An In-Depth Guide for Programmers 》] ( http://speakingjs.com/ ) 。
11+ 此外,《 Exploring ES6 》的作者对 JavaScript 有比较深入全面的了解 ,也出了一本 ES5 及之前版本的书[ 《 Speaking JavaScript: An In-Depth Guide for Programmers 》] ( http://speakingjs.com/ ) 。
1212
1313所以,总的来说,《 Exploring ES6 》还是值得一看的。
1414
Load Diff This file was deleted.
File renamed without changes.
Original file line number Diff line number Diff line change 1+ ## 6.2 Unicode 码点解析
2+
3+ 在 ECMAScript 6 中,存在一种新的 Unicode 码点解析方式:能够指定任何编码方式(即便这种编码方式超过了16位):
4+
5+ ``` js
6+ console .log (' \u{1F680} ' ); // ES6: 一个码点
7+ console .log (' \uD83D\uDE80 ' ); // ES5: 两个码点单元
8+ ```
9+
10+ 本章关于 Unicode 的部分展示了更多信息。
Original file line number Diff line number Diff line change 1+ ## 6.3 字符串插值,多行字符串字面量和原始字符串字面量
2+
3+ 模板字面量有专门的章节深入讲述,其提供了三种有意思的特性。
4+
5+ 首先,模板字面量支持字符串插值:
6+
7+ ``` js
8+ const first = ' Jane' ;
9+ const last = ' Doe' ;
10+ console .log (` Hello ${ first} ${ last} !` );
11+ // Hello Jane Doe!
12+ ```
13+
14+ 其次,模板字面量可以跨多行:
15+
16+ ``` js
17+ const multiLine = `
18+ This is
19+ a string
20+ with multiple
21+ lines` ;
22+ ```
23+
24+ 最后,如果在模板字面量前面加上* 标签* ` String.raw ` ,那么就变成了“原生的”的字符串了,反斜杠再也不是用于转义的特殊字符,也就是说, ` \n ` 不会转义成换行符:
25+
26+ ``` js
27+ const str = String .raw ` Not a newline: \n ` ;
28+ console .log (str === ' Not a newline: \\ n' ); // true
29+ ```
Original file line number Diff line number Diff line change 1+ ## 6.4 字符串遍历
2+
3+ 字符串是可遍历的,也就是说可以使用 ` for-of ` 遍历字符串中的字符:
4+
5+ ``` js
6+ for (const ch of ' abc' ) {
7+ console .log (ch);
8+ }
9+ // Output:
10+ // a
11+ // b
12+ // c
13+ ```
14+
15+ 也可以使用扩展操作符(...)将字符串转换成数组:
16+
17+ ``` js
18+ const chars = [... ' abc' ];
19+ // ['a', 'b', 'c']
20+ ```
21+
22+ ### 6.4.1 字符串遍历遵循 Unicode 码点规则
23+
24+ 字符串遍历的时候,分割字符串是按照码点边界来的,这意味着遍历的时候拿到的字符可能由一个或者两个 JavaScript 字符组成:
25+
26+ ``` js
27+ for (const ch of ' x\uD83D\uDE80 y' ) {
28+ console .log (ch .length );
29+ }
30+ // Output:
31+ // 1
32+ // 2
33+ // 1
34+ ```
35+
36+ ### 6.4.2 计算码点数
37+
38+ 遍历是一种快速计算字符串 Unicode 码点数的方式:
39+
40+ ```
41+ > [...'x\uD83D\uDE80y'].length
42+ 3
43+ ```
44+
45+ ### 6.4.3 翻转包含非 BMP 码点的字符串
46+
47+ 利用遍历也能够实现翻转包含非 BMP 码点(比16位大,用两个 JavaScript 字符编码)的字符串:
48+
49+ ``` js
50+ const str = ' x\uD83D\uDE80 y' ;
51+
52+ // ES5: \uD83D\uDE80 are (incorrectly) reversed
53+ console .log (str .split (' ' ).reverse ().join (' ' ));
54+ // 'y\uDE80\uD83Dx'
55+
56+ // ES6: order of \uD83D\uDE80 is preserved
57+ console .log ([... str].reverse ().join (' ' ));
58+ // 'y\uD83D\uDE80x'
59+ ```
60+
61+ 在火狐浏览器中的翻转结果:
62+
63+ ![ ] ( http://exploringjs.com/es6/images/strings----firefox_unicode_strings.jpg )
64+
65+ > 遗留问题:合并标记
66+ > 一个* 合并标记* 就是两个 Unicode 码点的序列,用于展示单个符号。我在这里展示的 ES6 翻转非 BMP 码点的方法,不适用于合并标记。对于合并标记的翻转,你需要一个库,例如 Mathias Bynens 的 [ Esrever] ( https://github.com/mathiasbynens/esrever ) 。
Original file line number Diff line number Diff line change 1+ ## 6.5 码点的数字值
2+
3+ 新方法 ` codePointAt() ` 返回字符串中指定位置字符的数字值:
4+
5+ ``` js
6+ const str = ' x\uD83D\uDE80 y' ;
7+ console .log (str .codePointAt (0 ).toString (16 )); // 78
8+ console .log (str .codePointAt (1 ).toString (16 )); // 1f680
9+ console .log (str .codePointAt (3 ).toString (16 )); // 79
10+ ```
11+
12+ 该方法在字符串遍历中也能正确执行:
13+
14+ ``` js
15+ for (const ch of ' x\uD83D\uDE80 y' ) {
16+ console .log (ch .codePointAt (0 ).toString (16 ));
17+ }
18+ // Output:
19+ // 78
20+ // 1f680
21+ // 79
22+ ```
23+
24+ 与 ` codePointAt() ` 对应的方法是 ` String.fromCodePoint() ` :
25+
26+ ```
27+ > String.fromCodePoint(0x78, 0x1f680, 0x79) === 'x\uD83D\uDE80y'
28+ true
29+ ```
Original file line number Diff line number Diff line change 1+ ## 6.6 检查子串
2+
3+ 有三个方法用于检查某个字符串是否是另一个字符串的子串:
4+
5+ ```
6+ > 'hello'.startsWith('hell')
7+ true
8+ > 'hello'.endsWith('ello')
9+ true
10+ > 'hello'.includes('ell')
11+ true
12+ ```
13+
14+ 每个方法都有可选的第二个参数,用于指定字符串搜索的开始或者结束位置:
15+
16+ ```
17+ > 'hello'.startsWith('ello', 1)
18+ true
19+ > 'hello'.endsWith('hell', 4)
20+ true
21+
22+ > 'hello'.includes('ell', 1)
23+ true
24+ > 'hello'.includes('ell', 2)
25+ false
26+ ```
Original file line number Diff line number Diff line change 1+ ## 6.7 重复字符串
2+
3+ ` repeat() ` 方法用于生成重复字符串:
4+
5+ ```
6+ > 'doo '.repeat(3)
7+ 'doo doo doo '
8+ ```
Original file line number Diff line number Diff line change 1+ ## 6.8 用正则表达式作为参数的字符串方法
2+
3+ 在 ES6 中,四个用正则表达式作为参数的字符串方法做的事情相当少,它们主要调用参数上面的方法:
4+
5+ * ` String.prototype.match(regexp) ` 调用 ` regexp[Symbol.match](this) ` 。
6+ * ` String.prototype.replace(searchValue, replaceValue) ` 调用 ` searchValue[Symbol.replace](this, replaceValue) ` 。
7+ * ` String.prototype.search(regexp) ` 调用 ` regexp[Symbol.search](this) ` 。
8+ * ` String.prototype.split(separator, limit) ` 调用 ` separator[Symbol.split](this, limit) ` 。
9+
10+ 这些参数根本不需要是正则表达式,任何带有相应方法的对象都可以作为参数。
You can’t perform that action at this time.
0 commit comments