Skip to content

Commit 7a85871

Browse files
committed
docs(functio): optional catch binding
1 parent fb2930d commit 7a85871

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

docs/function.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,3 +1319,40 @@ clownsEverywhere(
13191319

13201320
这样的规定也使得,函数参数与数组和对象的尾逗号规则,保持一致了。
13211321

1322+
## catch 语句的参数
1323+
1324+
目前,有一个[提案](https://github.com/tc39/proposal-optional-catch-binding),允许`try...catch`结构中的`catch`语句调用时不带有参数。这个提案跟参数有关,也放在这一章介绍。
1325+
1326+
传统的写法是`catch`语句必须带有参数,用来接收`try`代码块抛出的错误。
1327+
1328+
```javascript
1329+
try {
1330+
// ···
1331+
} catch (error) {
1332+
// ···
1333+
}
1334+
```
1335+
1336+
新的写法允许省略`catch`后面的参数,而不报错。
1337+
1338+
```javascript
1339+
try {
1340+
// ···
1341+
} catch {
1342+
// ···
1343+
}
1344+
```
1345+
1346+
新写法只在不需要错误实例的情况下有用,因此不及传统写法的用途广。
1347+
1348+
```javascript
1349+
let jsonData;
1350+
try {
1351+
jsonData = JSON.parse(str);
1352+
} catch {
1353+
jsonData = DEFAULT_DATA;
1354+
}
1355+
```
1356+
1357+
上面代码中,`JSON.parse`报错只有一种可能:解析失败。因此,可以不需要抛出的错误实例。
1358+

docs/reference.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
- Derick Bailey, [Do ES6 Arrow Functions Really Solve “this” In JavaScript?](http://derickbailey.com/2015/09/28/do-es6-arrow-functions-really-solve-this-in-javascript/):使用箭头函数处理this指向,必须非常小心
9191
- Mark McDonnell, [Understanding recursion in functional JavaScript programming](http://www.integralist.co.uk/posts/js-recursion.html): 如何自己实现尾递归优化
9292
- Nicholas C. Zakas, [The ECMAScript 2016 change you probably don't know](https://www.nczonline.net/blog/2016/10/the-ecmascript-2016-change-you-probably-dont-know/): 使用参数默认值时,不能在函数内部显式开启严格模式
93+
- Axel Rauschmayer, [ES proposal: optional catch binding](http://2ality.com/2017/08/optional-catch-binding.html)
9394

9495
## 对象
9596

0 commit comments

Comments
 (0)