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

docs: consistent style

This commit is contained in:
Ika 2017-11-12 18:01:54 +08:00
parent eeba1984a8
commit e8fdb00040
30 changed files with 614 additions and 639 deletions

View File

@ -816,4 +816,3 @@ for (let i of arr) {
```
由于空位的处理规则非常不统一,所以建议避免出现空位。

View File

@ -18,17 +18,17 @@
TypedArray 视图支持的数据类型一共有 9 种(`DataView`视图支持除`Uint8C`以外的其他 8 种)。
数据类型 | 字节长度 | 含义 | 对应的C语言类型
--------|--------|----|---------------
Int8|1|8位带符号整数|signed char
Uint8|1|8位不带符号整数|unsigned char
Uint8C|1|8位不带符号整数自动过滤溢出|unsigned char
Int16|2|16位带符号整数|short
Uint16|2|16位不带符号整数|unsigned short
Int32|4|32位带符号整数|int
Uint32|4|32位不带符号的整数|unsigned int
Float32|4|32位浮点数|float
Float64|8|64位浮点数|double
| 数据类型 | 字节长度 | 含义 | 对应的 C 语言类型 |
| -------- | -------- | -------------------------------- | ----------------- |
| Int8 | 1 | 8 位带符号整数 | signed char |
| Uint8 | 1 | 8 位不带符号整数 | unsigned char |
| Uint8C | 1 | 8 位不带符号整数(自动过滤溢出) | unsigned char |
| Int16 | 2 | 16 位带符号整数 | short |
| Uint16 | 2 | 16 位不带符号整数 | unsigned short |
| Int32 | 4 | 32 位带符号整数 | int |
| Uint32 | 4 | 32 位不带符号的整数 | unsigned int |
| Float32 | 4 | 32 位浮点数 | float |
| Float64 | 8 | 64 位浮点数 | double |
注意,二进制数组并不是真正的数组,而是类似数组的对象。
@ -1225,4 +1225,3 @@ Atomics.xor(sharedArray, index, value)
- `Atomics.isLockFree(size)`:返回一个布尔值,表示`Atomics`对象是否可以处理某个`size`的内存锁定。如果返回`false`,应用程序就需要自己来实现锁定。
`Atomics.compareExchange`的一个用途是,从 SharedArrayBuffer 读取一个值,然后对该值进行某个操作,操作结束以后,检查一下 SharedArrayBuffer 里面原来那个值是否发生变化(即被其他线程改写过)。如果没有改写过,就将它写回原来的位置,否则读取新的值,再重头进行一次操作。

View File

@ -988,7 +988,7 @@ async function* createAsyncIterable(syncIterable) {
上面代码中,由于没有异步操作,所以也就没有使用`await`关键字。
### yield* 语句
### yield\* 语句
`yield*`语句也可以跟一个异步遍历器。
@ -1018,4 +1018,3 @@ async function* gen2() {
// a
// b
```

View File

@ -712,4 +712,3 @@ class DistributedEdit extends mix(Loggable, Serializable) {
// ...
}
```

View File

@ -930,4 +930,3 @@ var y = new Rectangle(3, 4); // 正确
上面代码中,`Shape`类不能被实例化,只能用于继承。
注意,在函数外部,使用`new.target`会报错。

View File

@ -792,4 +792,3 @@ babel.transform("code", {plugins: ["transform-decorators"]})
```
Babel 的官方网站提供一个[在线转码器](https://babeljs.io/repl/),只要勾选 Experimental就能支持 Decorator 的在线转码。

View File

@ -1365,4 +1365,3 @@ try {
```
上面代码中,`JSON.parse`报错只有一种可能:解析失败。因此,可以不需要抛出的错误实例。

View File

@ -788,4 +788,3 @@ co(function*() {
```
上面代码采用 Stream 模式读取《悲惨世界》的文本文件,对于每个数据块都使用`stream.once`方法,在`data``end``error`三个事件上添加一次性回调函数。变量`res`只有在`data`事件发生时才有值,然后累加每个数据块之中`valjean`这个词出现的次数。

View File

@ -744,7 +744,7 @@ gen.return(2); // Object {value: 2, done: true}
// 替换成 let result = return 2;
```
## yield* 表达式
## yield\* 表达式
如果在 Generator 函数内部,调用另一个 Generator 函数,默认情况下是没有效果的。
@ -1471,4 +1471,3 @@ function doStuff() {
```
上面的函数,可以用一模一样的`for...of`循环处理!两相一比较,就不难看出 Generator 使得数据或者操作,具备了类似数组的接口。

View File

@ -586,4 +586,3 @@ fs.writeFileSync('out.js', result.js);
// sourceMap 属性对应 map 文件
fs.writeFileSync('out.js.map', result.sourceMap);
```

View File

@ -363,7 +363,7 @@ let arr = ['b', 'c'];
let arr = [...iterable];
```
**3yield* **
**3yield\* **
`yield*`后面跟的是一个可遍历的结构,它会调用该结构的遍历器接口。
@ -820,4 +820,3 @@ for (var n of fibonacci) {
```
上面的例子,会输出斐波纳契数列小于等于 1000 的项。如果当前项大于 1000就会使用`break`语句跳出`for...of`循环。

View File

@ -662,4 +662,3 @@ const global = getGlobal();
```
上面代码将顶层对象放入变量`global`

View File

@ -812,4 +812,3 @@ System.import('app/es6-file').then(function(m) {
```
上面代码中,`System.import`方法返回的是一个 Promise 对象,所以可以用`then`方法指定回调函数。

View File

@ -784,4 +784,3 @@ async function main() {
}
main();
```

View File

@ -791,4 +791,3 @@ Integer 类型不能与 Number 类型进行混合运算。
0n === 0
// false
```

View File

@ -1510,4 +1510,3 @@ a?.b = 42
// 如果 a 是 null 或 undefined下面的语句不产生任何效果
delete a?.b
```

View File

@ -976,4 +976,3 @@ Promise.try(database.users.get({id: userId}))
```
事实上,`Promise.try`就是模拟`try`代码块,就像`promise.catch`模拟的是`catch`代码块。

View File

@ -1049,4 +1049,3 @@ function createWebService(baseUrl) {
```
同理Proxy 也可以用来实现数据库的 ORM 层。

View File

@ -249,4 +249,3 @@
- SystemJS, [SystemJS](https://github.com/systemjs/systemjs): 在浏览器中加载 AMD、CJS、ES6 模块的一个垫片库
- Modernizr, [HTML5 Cross Browser Polyfills](https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-Browser-Polyfills#ecmascript-6-harmony): ES6 垫片库清单
- Facebook, [regenerator](https://github.com/facebook/regenerator): 将 Generator 函数转为 ES5 的转码器

View File

@ -517,4 +517,3 @@ function set(target, key, value, receiver) {
```
上面代码中,先定义了一个`Set`集合,所有观察者函数都放进这个集合。然后,`observable`函数返回原始对象的代理,拦截赋值操作。拦截函数`set`之中,会自动执行所有观察者。

View File

@ -581,4 +581,3 @@ const RE_TWICE = /^(?<word>[a-z]+)!\k<word>!\1$/;
RE_TWICE.test('abc!abc!abc') // true
RE_TWICE.test('abc!abc!ab') // false
```

View File

@ -713,4 +713,3 @@ function average(list) {
```
上面代码先是每隔四位,将所有的值读入一个 SIMD然后立刻累加。然后得到累加值四个通道的总和再除以`n`就可以了。

View File

@ -40,19 +40,19 @@ ECMAScript 6规格的26章之中第1章到第3章是对文件本身的介绍
> 1. ReturnIfAbrupt(x).
> 1. ReturnIfAbrupt(y).
> 1. If `Type(x)` is the same as `Type(y)`, then
> 1. If `Type(x)` is the same as `Type(y)`, then\
> Return the result of performing Strict Equality Comparison `x === y`.
> 1. If `x` is `null` and `y` is `undefined`, return `true`.
> 1. If `x` is `undefined` and `y` is `null`, return `true`.
> 1. If `Type(x)` is Number and `Type(y)` is String,
> 1. If `Type(x)` is Number and `Type(y)` is String,\
> return the result of the comparison `x == ToNumber(y)`.
> 1. If `Type(x)` is String and `Type(y)` is Number,
> 1. If `Type(x)` is String and `Type(y)` is Number,\
> return the result of the comparison `ToNumber(x) == y`.
> 1. If `Type(x)` is Boolean, return the result of the comparison `ToNumber(x) == y`.
> 1. If `Type(y)` is Boolean, return the result of the comparison `x == ToNumber(y)`.
> 1. If `Type(x)` is either String, Number, or Symbol and `Type(y)` is Object, then
> 1. If `Type(x)` is either String, Number, or Symbol and `Type(y)` is Object, then\
> return the result of the comparison `x == ToPrimitive(y)`.
> 1. If `Type(x)` is Object and `Type(y)` is either String, Number, or Symbol, then
> 1. If `Type(x)` is Object and `Type(y)` is either String, Number, or Symbol, then\
> return the result of the comparison `ToPrimitive(x) == y`.
> 1. Return `false`.
@ -145,17 +145,17 @@ a2.map(n => 1) // [, , ,]
> 1. Let `A` be `ArraySpeciesCreate(O, len)`.
> 1. `ReturnIfAbrupt(A)`.
> 1. Let `k` be 0.
> 1. Repeat, while `k` < `len`
> a. Let `Pk` be `ToString(k)`.
> b. Let `kPresent` be `HasProperty(O, Pk)`.
> c. `ReturnIfAbrupt(kPresent)`.
> d. If `kPresent` is `true`, then
> d-1. Let `kValue` be `Get(O, Pk)`.
> d-2. `ReturnIfAbrupt(kValue)`.
> d-3. Let `mappedValue` be `Call(callbackfn, T, «kValue, k, O»)`.
> d-4. `ReturnIfAbrupt(mappedValue)`.
> d-5. Let `status` be `CreateDataPropertyOrThrow (A, Pk, mappedValue)`.
> d-6. `ReturnIfAbrupt(status)`.
> 1. Repeat, while `k` < `len`\
> a. Let `Pk` be `ToString(k)`.\
> b. Let `kPresent` be `HasProperty(O, Pk)`.\
> c. `ReturnIfAbrupt(kPresent)`.\
> d. If `kPresent` is `true`, then\
> d-1. Let `kValue` be `Get(O, Pk)`.\
> d-2. `ReturnIfAbrupt(kValue)`.\
> d-3. Let `mappedValue` be `Call(callbackfn, T, «kValue, k, O»)`.\
> d-4. `ReturnIfAbrupt(mappedValue)`.\
> d-5. Let `status` be `CreateDataPropertyOrThrow (A, Pk, mappedValue)`.\
> d-6. `ReturnIfAbrupt(status)`.\
> e. Increase `k` by 1.
> 1. Return `A`.
@ -170,17 +170,17 @@ a2.map(n => 1) // [, , ,]
> 1. 生成一个新的数组`A`,跟当前数组的`length`属性保持一致
> 1. 如果报错就返回
> 1. 设定`k`等于 0
> 1. 只要`k`小于当前数组的`length`属性,就重复下面步骤
> a. 设定`Pk`等于`ToString(k)`,即将`K`转为字符串
> b. 设定`kPresent`等于`HasProperty(O, Pk)`,即求当前数组有没有指定属性
> c. 如果报错就返回
> d. 如果`kPresent`等于`true`,则进行下面步骤
> d-1. 设定`kValue`等于`Get(O, Pk)`,取出当前数组的指定属性
> d-2. 如果报错就返回
> d-3. 设定`mappedValue`等于`Call(callbackfn, T, «kValue, k, O»)`,即执行回调函数
> d-4. 如果报错就返回
> d-5. 设定`status`等于`CreateDataPropertyOrThrow (A, Pk, mappedValue)`,即将回调函数的值放入`A`数组的指定位置
> d-6. 如果报错就返回
> 1. 只要`k`小于当前数组的`length`属性,就重复下面步骤\
> a. 设定`Pk`等于`ToString(k)`,即将`K`转为字符串\
> b. 设定`kPresent`等于`HasProperty(O, Pk)`,即求当前数组有没有指定属性\
> c. 如果报错就返回\
> d. 如果`kPresent`等于`true`,则进行下面步骤\
> d-1. 设定`kValue`等于`Get(O, Pk)`,取出当前数组的指定属性\
> d-2. 如果报错就返回\
> d-3. 设定`mappedValue`等于`Call(callbackfn, T, «kValue, k, O»)`,即执行回调函数\
> d-4. 如果报错就返回\
> d-5. 设定`status`等于`CreateDataPropertyOrThrow (A, Pk, mappedValue)`,即将回调函数的值放入`A`数组的指定位置\
> d-6. 如果报错就返回\
> e. `k`增加 1
> 1. 返回`A`

View File

@ -409,7 +409,6 @@ $('#list').html(`
上面代码中,所有模板字符串的空格和换行,都是被保留的,比如`<ul>`标签前面会有一个换行。如果你不想要这个换行,可以使用`trim`方法消除它。
```javascript
$('#list').html(`
<ul>
@ -785,7 +784,6 @@ message
// <p>&lt;script&gt;alert("abc")&lt;/script&gt; has sent you a message.</p>
```
标签模板的另一个应用,就是多语言转换(国际化处理)。
```javascript
@ -948,4 +946,3 @@ tag`\unicode and \u{55}`
```javascript
let bad = `bad escape sequence: \unicode`; // 报错
```