Skip to content

Commit c6d6fa3

Browse files
committed
edit errors
1 parent 3da320f commit c6d6fa3

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

docs/function.md

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ function fetch(url, { body = '', method = 'GET', headers = {} }){
6666

6767
fetch('http://example.com', {})
6868
// "GET"
69+
70+
fetch('http://example.com')
71+
// 报错
6972
```
7073

7174
上面代码中,传入函数`fetch`的第二个参数是一个对象,调用的时候可以为它的三个属性设置默认值。
@@ -83,18 +86,32 @@ fetch('http://example.com')
8386

8487
上面代码中,调用函数`fetch`时,第二个参数默认为一个空对象,而只要有第二个参数,`method`参数就默认为`GET`
8588

86-
定义了默认值的参数,必须是函数的尾参数,其后不能再有其他无默认值的参数。这是因为有了默认值以后,该参数可以省略,只有位于尾部,才可能判断出到底省略了哪些参数
89+
通常情况下,定义了默认值的参数,都是函数的尾参数。因为这样比较容易看出来,到底省略了哪些参数。但是,非尾部的参数,也是可以设置默认值的
8790

8891
```javascript
89-
// 以下两种写法都是错的
90-
91-
function f(x = 5, y) {
92+
// 例一
93+
function f(x=1, y) {
94+
return [x, y];
9295
}
9396

97+
f() // [1, undefined]
98+
f(2) // [2, undefined])
99+
f(, 1) // 报错
100+
f(undefined, 1) // [1, 1]
101+
102+
// 例二
94103
function f(x, y = 5, z) {
104+
return [x, y, z];
95105
}
106+
107+
f() // [undefined, 5, undefined]
108+
f(1) // [1, 5, undefined]
109+
f(1,,2) // 报错
110+
f(1, undefined, 2) // [1, 5, 2]
96111
```
97112

113+
上面代码中,有默认值的参数都不是尾参数。这时,无法只省略该参数,而不省略它后面的参数,除非显式输入`undefined`
114+
98115
如果传入`undefined`,将触发该参数等于默认值,null则没有这个效果。
99116

100117
```javascript

docs/iterator.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ NodeList.prototype[Symbol.iterator] = [][Symbol.iterator];
233233
[...document.querySelectorAll('div')] // 可以执行了
234234
```
235235

236-
下面是对象调用数组的`Symbol.iterator`方法的例子。
236+
下面是类似数组的对象调用数组的`Symbol.iterator`方法的例子。
237237

238238
```javascript
239239
let iterable = {
@@ -248,6 +248,21 @@ for (let item of iterable) {
248248
}
249249
```
250250

251+
注意,普通对象部署数组的`Symbol.iterator`方法,并无效果。
252+
253+
```javascript
254+
let iterable = {
255+
a: 'a',
256+
b: 'b',
257+
c: 'c',
258+
length: 3,
259+
[Symbol.iterator]: Array.prototype[Symbol.iterator]
260+
};
261+
for (let item of iterable) {
262+
console.log(item); // undefined, undefined, undefined
263+
}
264+
```
265+
251266
如果`Symbol.iterator`方法对应的不是遍历器生成函数(即会返回一个遍历器对象),解释引擎将会报错。
252267

253268
```javascript

docs/promise.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ function run (generator) {
526526
return result.value.then(function (value) {
527527
return go(it.next(value));
528528
}, function (error) {
529-
return go(it.throw(value));
529+
return go(it.throw(error));
530530
});
531531
}
532532

0 commit comments

Comments
 (0)