Skip to content

Commit

Permalink
第六章
Browse files Browse the repository at this point in the history
  • Loading branch information
yibuyisheng committed Mar 24, 2016
1 parent 1ab2a2d commit f962d74
Show file tree
Hide file tree
Showing 12 changed files with 247 additions and 49 deletions.
1 change: 0 additions & 1 deletion md/6.2.md

This file was deleted.

File renamed without changes.
10 changes: 10 additions & 0 deletions md/6/6.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## 6.2 Unicode 码点解析

在 ECMAScript 6 中,存在一种新的 Unicode 码点解析方式:能够指定任何编码方式(即便这种编码方式超过了16位):

```js
console.log('\u{1F680}'); // ES6: 一个码点
console.log('\uD83D\uDE80'); // ES5: 两个码点单元
```

本章关于 Unicode 的部分展示了更多信息。
29 changes: 29 additions & 0 deletions md/6/6.3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
## 6.3 字符串插值,多行字符串字面量和原始字符串字面量

模板字面量有专门的章节深入讲述,其提供了三种有意思的特性。

首先,模板字面量支持字符串插值:

```js
const first = 'Jane';
const last = 'Doe';
console.log(`Hello ${first} ${last}!`);
// Hello Jane Doe!
```

其次,模板字面量可以跨多行:

```js
const multiLine = `
This is
a string
with multiple
lines`;
```

最后,如果在模板字面量前面加上*标签* `String.raw` ,那么就变成了“原生的”的字符串了,反斜杠再也不是用于转义的特殊字符,也就是说, `\n` 不会转义成换行符:

```js
const str = String.raw`Not a newline: \n`;
console.log(str === 'Not a newline: \\n'); // true
```
66 changes: 66 additions & 0 deletions md/6/6.4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
## 6.4 字符串遍历

字符串是可遍历的,也就是说可以使用 `for-of` 遍历字符串中的字符:

```js
for (const ch of 'abc') {
console.log(ch);
}
// Output:
// a
// b
// c
```

也可以使用扩展操作符(...)将字符串转换成数组:

```js
const chars = [...'abc'];
// ['a', 'b', 'c']
```

### 6.4.1 字符串遍历遵循 Unicode 码点规则

字符串遍历的时候,分割字符串是按照码点边界来的,这意味着遍历的时候拿到的字符可能由一个或者两个 JavaScript 字符组成:

```js
for (const ch of 'x\uD83D\uDE80y') {
console.log(ch.length);
}
// Output:
// 1
// 2
// 1
```

### 6.4.2 计算码点数

遍历是一种快速计算字符串 Unicode 码点数的方式:

```
> [...'x\uD83D\uDE80y'].length
3
```

### 6.4.3 翻转包含非 BMP 码点的字符串

利用遍历也能够实现翻转包含非 BMP 码点(比16位大,用两个 JavaScript 字符编码)的字符串:

```js
const str = 'x\uD83D\uDE80y';

// ES5: \uD83D\uDE80 are (incorrectly) reversed
console.log(str.split('').reverse().join(''));
// 'y\uDE80\uD83Dx'

// ES6: order of \uD83D\uDE80 is preserved
console.log([...str].reverse().join(''));
// 'y\uD83D\uDE80x'
```

在火狐浏览器中的翻转结果:

![](http://exploringjs.com/es6/images/strings----firefox_unicode_strings.jpg)

> 遗留问题:合并标记
> 一个*合并标记*就是两个 Unicode 码点的序列,用于展示单个符号。我在这里展示的 ES6 翻转非 BMP 码点的方法,不适用于合并标记。对于合并标记的翻转,你需要一个库,例如 Mathias Bynens 的 [Esrever](https://github.com/mathiasbynens/esrever)
29 changes: 29 additions & 0 deletions md/6/6.5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
## 6.5 码点的数字值

新方法 `codePointAt()` 返回字符串中指定位置字符的数字值:

```js
const str = 'x\uD83D\uDE80y';
console.log(str.codePointAt(0).toString(16)); // 78
console.log(str.codePointAt(1).toString(16)); // 1f680
console.log(str.codePointAt(3).toString(16)); // 79
```

该方法在字符串遍历中也能正确执行:

```js
for (const ch of 'x\uD83D\uDE80y') {
console.log(ch.codePointAt(0).toString(16));
}
// Output:
// 78
// 1f680
// 79
```

`codePointAt()` 对应的方法是 `String.fromCodePoint()`

```
> String.fromCodePoint(0x78, 0x1f680, 0x79) === 'x\uD83D\uDE80y'
true
```
26 changes: 26 additions & 0 deletions md/6/6.6.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
## 6.6 检查子串

有三个方法用于检查某个字符串是否是另一个字符串的子串:

```
> 'hello'.startsWith('hell')
true
> 'hello'.endsWith('ello')
true
> 'hello'.includes('ell')
true
```

每个方法都有可选的第二个参数,用于指定字符串搜索的开始或者结束位置:

```
> 'hello'.startsWith('ello', 1)
true
> 'hello'.endsWith('hell', 4)
true
> 'hello'.includes('ell', 1)
true
> 'hello'.includes('ell', 2)
false
```
8 changes: 8 additions & 0 deletions md/6/6.7.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## 6.7 重复字符串

`repeat()` 方法用于生成重复字符串:

```
> 'doo '.repeat(3)
'doo doo doo '
```
10 changes: 10 additions & 0 deletions md/6/6.8.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## 6.8 用正则表达式作为参数的字符串方法

在 ES6 中,四个用正则表达式作为参数的字符串方法做的事情相当少,它们主要调用参数上面的方法:

* `String.prototype.match(regexp)` 调用 `regexp[Symbol.match](this)`
* `String.prototype.replace(searchValue, replaceValue)` 调用 `searchValue[Symbol.replace](this, replaceValue)`
* `String.prototype.search(regexp)` 调用 `regexp[Symbol.search](this)`
* `String.prototype.split(separator, limit)` 调用 `separator[Symbol.split](this, limit)`

这些参数根本不需要是正则表达式,任何带有相应方法的对象都可以作为参数。
49 changes: 49 additions & 0 deletions md/6/6.9.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
## 6.9 备忘:新的字符串方法

标签模板:

* String.raw(callSite, ...substitutions) : string

用于获取“原始”字符串内容的模板标签(反斜杠不再是转义字符):

```
> String.raw`\` === '\\'
true
```
更多内容可阅读关于模板字面量的章节。
Unicode 和码点:
* String.fromCodePoint(...codePoints : number[]) : string
将数字值转换成 Unicode 码点字,然后返回由码点构成的字符串。
* String.prototype.codePointAt(pos) : number
返回在从位置 `pos` 处开始的码点的数字值(由一个或者两个 JavaScript 字符组成)。
* String.prototype.normalize(form? : string) : string
不同的码点组合可能看起来是一样的。 [Unicode 标准化](http://unicode.org/faq/normalization.html) 将它们修正为相同的*标准值*。这对相等比较和字符串搜索很有帮助。对于一般的文本,建议使用 `NFC` 形式。
查找字符串:
* String.prototype.startsWith(searchString, position=0) : boolean
`position` 参数指定了字符串的开始搜索位置。
* String.prototype.endsWith(searchString, endPosition=searchString.length) : boolean
`endPosition` 指定了字符串的结束搜索位置。
* String.prototype.includes(searchString, position=0) : boolean
从字符串 `position` 位置开始搜索,是否包含 `searchString` 子串。
重复字符串:
* String.prototype.repeat(count) : string
返回重复指定次数的字符串。
File renamed without changes.
68 changes: 20 additions & 48 deletions md/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,6 @@
* 感谢

* I 背景
<<<<<<< HEAD
* 1 [关于 ECMAScript 6 ( ES6 )](./1.md)
* 1.1 [TC39 ( Ecma 技术委员会 39 )](./1.1.md)
* 1.2 [ECMAScript 6 是如何设计的](./1.2.md)
* 1.3 [JavaScript vs ECMAScript](./1.3.md)
* 1.4 [升级到 ES6](./1.4.md)
* 1.5 [ES6 的目标](./1.5.md)
* 1.6 [ES6 特性概览](./1.6.md)
* 1.7 [ECMAScript 简史](./1.7.md)
* 2 [ECMAScript 6 常见问题解答](./2.md)
* 2.1 [当前引擎支持 ES6 情况如何?](./2.1.md)
* 2.2 [如何将 ECMAScript 5 代码升级至 ECMAScript 6 ?](./2.2.md)
* 2.3 [现在学习 ECMAScript 5 还有意义吗?](./2.3.md)
* 2.4 [ES6 臃肿吗?](./2.4.md)
* 2.5 [ES6 规范文档不是很长吗?](./2.5.md)
* 2.6 [ES6 包含了数组生成表达式( Array Comprehension )吗?](./2.6.md)
* 2.7 [ES6 是静态类型的吗?](./2.7.md)
* 2.8 [应该避免使用类吗?](./2.8.md)
* 2.9 [ES6 有`特性`( traits )或者`混入`( mixins )吗?](./2.9.md)
* 2.10 [为什么 ES6 有带 `=>` 的箭头函数,而没有带 `->` 的箭头函数?](./2.10.md)
* 2.11 [在哪里可以找到更多的 ES6 资源?](./2.11.md)
* 3 [一个 JavaScript : 在 ECMAScript 6 中避免版本化](./3.md)
* 3.1 [版本化](./3.1.md)
* 3.2 [严格模式与 ECMAScript 6](./3.2.md)
* 3.3 [结语](./3.3.md)
* 3.4 [深入阅读](./3.4.md)
* 4 [进入 ECMAScript 6 的第一步](./4.md)
* 4.1 [尝试 ECMAScript 6](./4.1.md)
* 4.2 转换工具
* 4.3 其他有用的 ES6 工具和库
* 4.4 ES6 交互式解释器( REPLs )
* 4.5 有 ES6 特性不能转换成 ES5 吗?
* 4.6 示范转换设置
* 4.7 示范设置:通过 webpack 和 babel 处理客户端 ES6
* 4.8 示范设置:通过 Babel 在 Node.js 基础上动态转换 ES6
* 4.9 示范设置:通过 gulp 和 Babel 在 Node.js 上静态转换 ES6
=======
* 1 [关于 ECMAScript 6 ( ES6 )](./1/1.md)
* 1.1 [TC39 ( Ecma 技术委员会 39 )](./1/1.1.md)
* 1.2 [ECMAScript 6 是如何设计的](./1/1.2.md)
Expand All @@ -71,24 +34,33 @@
* 3.2 [严格模式与 ECMAScript 6](./3/3.2.md)
* 3.3 [总结](./3/3.3.md)
* 3.4 [深入阅读](./3/3.4.md)
* 4 [ECMAScript 6 入门](./4/4.md)
* 5 [部署 ECMAScript 6](./5/5.md)
>>>>>>> gh-pages
* 4 [进入 ECMAScript 6 的第一步](./4.md)
* 4.1 [尝试 ECMAScript 6](./4.1.md)
* 4.2 转换工具
* 4.3 其他有用的 ES6 工具和库
* 4.4 ES6 交互式解释器( REPLs )
* 4.5 有 ES6 特性不能转换成 ES5 吗?
* 4.6 示范转换设置
* 4.7 示范设置:通过 webpack 和 babel 处理客户端 ES6
* 4.8 示范设置:通过 Babel 在 Node.js 基础上动态转换 ES6
* 4.9 示范设置:通过 gulp 和 Babel 在 Node.js 上静态转换 ES6
* II 数据
* 5 [新的数值和 Math 特性](./5.md)
* 5.1 概览
* 5.2 新的整型字面量
* 5.3 新的静态 Number 类属性
* 5.4 Math
* 5.5 常见问题解答
* 6 新的字符串特性
* 6.1 概览
* 6.2 Unicode 字符解析( Unicode code point escapes )
* 6.3 字符串插值,多行字符串字面量和原始的字符串字面量
* 6.4 遍历字符串
* 6.5 Numeric values of code points
* 6.6 检查字串包含和重复字符串
* 6.7 所有新的字符串方法
* 6 [新的字符串特性](./6/6.md)
* 6.1 [概览](./6/6.1.md)
* 6.2 [Unicode 字符解析( Unicode code point escapes )](./6/6.2.md)
* 6.3 [字符串插值,多行字符串字面量和原始的字符串字面量](./6/6.3.md)
* 6.4 [字符串遍历](./6/6.4.md)
* 6.5 [码点的数字值](./6/6.5.md)
* 6.6 [检查子串](./6/6.6.md)
* 6.7 [重复字符串](./6/6.7.md)
* 6.8 [用正则表达式作为参数的字符串方法](./6/6.8.md)
* 6.9 [备忘:新的字符串方法](./6/6.9.md)
* 7 Symbols
* 7.1 概览
* 7.2 新的原始类型
Expand Down

0 comments on commit f962d74

Please sign in to comment.