Skip to content

Commit e657a67

Browse files
committed
docs(generator): edit generator
1 parent a0300bd commit e657a67

File tree

4 files changed

+13
-45
lines changed

4 files changed

+13
-45
lines changed

docs/generator.md

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -537,47 +537,9 @@ try {
537537
538538
上面代码中,`throw`命令抛出的错误不会影响到遍历器的状态,所以两次执行`next`方法,都取到了正确的操作。
539539
540-
这种函数体内捕获错误的机制,大大方便了对错误的处理。如果使用回调函数的写法,想要捕获多个错误,就不得不为每个函数写一个错误处理语句
540+
这种函数体内捕获错误的机制,大大方便了对错误的处理。如果使用回调函数的写法,想要捕获多个错误,就不得不为每个函数内部写一个错误处理语句。现在可以只在Generator函数内部写一次`catch`语句
541541
542-
```javascript
543-
foo('a', function (a) {
544-
if (a.error) {
545-
throw new Error(a.error);
546-
}
547-
548-
foo('b', function (b) {
549-
if (b.error) {
550-
throw new Error(b.error);
551-
}
552-
553-
foo('c', function (c) {
554-
if (c.error) {
555-
throw new Error(c.error);
556-
}
557-
558-
console.log(a, b, c);
559-
});
560-
});
561-
});
562-
```
563-
564-
使用Generator函数可以大大简化上面的代码。
565-
566-
```javascript
567-
function* g(){
568-
try {
569-
var a = yield foo('a');
570-
var b = yield foo('b');
571-
var c = yield foo('c');
572-
} catch (e) {
573-
console.log(e);
574-
}
575-
576-
console.log(a, b, c);
577-
}
578-
```
579-
580-
反过来,Generator函数内抛出的错误,也可以被函数体外的`catch`捕获。
542+
Generator函数体外抛出的错误,可以在函数体内捕获;反过来,Generator函数体内抛出的错误,也可以被函数体外的`catch`捕获。
581543
582544
```javascript
583545
function *foo() {

docs/promise.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,18 @@ var p2 = new Promise(function (resolve, reject) {
168168
var p1 = new Promise(function (resolve, reject) {
169169
setTimeout(() => reject(new Error('fail')), 3000)
170170
})
171+
171172
var p2 = new Promise(function (resolve, reject) {
172173
setTimeout(() => resolve(p1), 1000)
173174
})
174-
p2.then(result => console.log(result))
175-
p2.catch(error => console.log(error))
175+
176+
p2
177+
.then(result => console.log(result))
178+
.catch(error => console.log(error))
176179
// Error: fail
177180
```
178181

179-
上面代码中,`p1`是一个Promise,3秒之后变为`rejected``p2`的状态由`p1`决定,1秒之后,`p2`调用`resolve`方法,但是此时`p1`的状态还没有改变,因此`p2`的状态也不会变。又过了2秒,`p1`变为`rejected``p2`也跟着变为`rejected`
182+
上面代码中,`p1`是一个Promise,3秒之后变为`rejected``p2`的状态在1秒之后改变,`resolve`方法返回的是`p1`。此时,由于`p2`返回的是另一个Promise,所以后面的`then`语句都变成针对后者(`p1`。又过了2秒,`p1`变为`rejected`导致触发`catch`方法指定的回调函数
180183

181184
## Promise.prototype.then()
182185

docs/proxy.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ fproxy.foo // "Hello, foo"
127127

128128
**(1)get(target, propKey, receiver)**
129129

130-
拦截对象属性的读取,比如`proxy.foo``proxy['foo']`,返回类型不限。最后一个参数`receiver`可选,当`target`对象设置了`propKey`属性的`get`函数时,`receiver`对象会绑定`get`函数的`this`对象。
130+
拦截对象属性的读取,比如`proxy.foo``proxy['foo']`
131+
132+
最后一个参数`receiver`是一个对象,可选,参见下面`Reflect.get`的部分。
131133

132134
**(2)set(target, propKey, value, receiver)**
133135

docs/symbol.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,13 +402,14 @@ class MyClass {
402402
```javascript
403403
let arr1 = ['c', 'd'];
404404
['a', 'b'].concat(arr1, 'e') // ['a', 'b', 'c', 'd', 'e']
405+
arr1[Symbol.isConcatSpreadable] // undefined
405406

406407
let arr2 = ['c', 'd'];
407408
arr2[Symbol.isConcatSpreadable] = false;
408409
['a', 'b'].concat(arr2, 'e') // ['a', 'b', ['c','d'], 'e']
409410
```
410411

411-
上面代码说明,数组的`Symbol.isConcatSpreadable`属性默认为`true`,表示可以展开
412+
上面代码说明,数组的默认行为是可以展开。`Symbol.isConcatSpreadable`属性等于`true``undefined`,都有这个效果
412413

413414
类似数组的对象也可以展开,但它的`Symbol.isConcatSpreadable`属性默认为`false`,必须手动打开。
414415

0 commit comments

Comments
 (0)