Skip to content

Commit 81e186f

Browse files
committed
docs(function): fix 函数的默认参数
1 parent d4ae227 commit 81e186f

File tree

2 files changed

+8
-13
lines changed

2 files changed

+8
-13
lines changed

docs/function.md

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,12 @@ log('Hello', '') // Hello World
1919

2020
上面代码检查函数`log`的参数`y`有没有赋值,如果没有,则指定默认值为`World`。这种写法的缺点在于,如果参数`y`赋值了,但是对应的布尔值为`false`,则该赋值不起作用。就像上面代码的最后一行,参数`y`等于空字符,结果被改为默认值。
2121

22-
为了避免这个问题,通常需要先判断一下参数y是否被赋值,如果没有,再等于默认值。这有两种写法
22+
为了避免这个问题,通常需要先判断一下参数`y`是否被赋值,如果没有,再等于默认值。
2323

2424
```javascript
25-
// 写法一
2625
if (typeof y === 'undefined') {
2726
y = 'World';
2827
}
29-
30-
// 写法二
31-
if (arguments.length === 1) {
32-
y = 'World';
33-
}
3428
```
3529

3630
ES6允许为函数的参数设置默认值,即直接写在参数定义的后面。
@@ -57,9 +51,9 @@ var p = new Point();
5751
p // { x: 0, y: 0 }
5852
```
5953

60-
除了简洁,ES6的写法还有两个好处:首先,阅读代码的人,可以立刻意识到哪些参数是可以省略的,不用查看函数体或文档;其次,有利于将来的代码优化,即使未来的版本彻底拿掉这个参数,也不会导致以前的代码无法运行。
54+
除了简洁,ES6的写法还有两个好处:首先,阅读代码的人,可以立刻意识到哪些参数是可以省略的,不用查看函数体或文档;其次,有利于将来的代码优化,即使未来的版本在对外接口中,彻底拿掉这个参数,也不会导致以前的代码无法运行。
6155

62-
参数变量是默认声明的,所以不能用let或const再次声明
56+
参数变量是默认声明的,所以不能用`let``const`再次声明
6357

6458
```javascript
6559
function foo(x = 5) {
@@ -68,7 +62,7 @@ function foo(x = 5) {
6862
}
6963
```
7064

71-
上面代码中,参数变量`x`是默认声明的,在函数体中,不能用let或const再次声明,否则会报错。
65+
上面代码中,参数变量`x`是默认声明的,在函数体中,不能用`let``const`再次声明,否则会报错。
7266

7367
### 与解构赋值默认值结合使用
7468

@@ -343,8 +337,9 @@ add(2, 5, 3) // 10
343337

344338
```javascript
345339
// arguments变量的写法
346-
const sortNumbers = () =>
347-
Array.prototype.slice.call(arguments).sort();
340+
function sortNumbers() {
341+
return Array.prototype.slice.call(arguments).sort();
342+
}
348343

349344
// rest参数的写法
350345
const sortNumbers = (...numbers) => numbers.sort();

docs/iterator.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ Obj.prototype[Symbol.iterator] = function(){
182182
function next(){
183183
if (current){
184184
var value = current.value;
185-
var done = current == null;
185+
var done = current === null;
186186
current = current.next;
187187
return {
188188
done: done,

0 commit comments

Comments
 (0)