docs(functio): optional catch binding

This commit is contained in:
ruanyf
2017-08-14 14:11:40 +08:00
parent fb2930d272
commit 7a85871d4a
2 changed files with 38 additions and 0 deletions
+37
View File
@@ -1319,3 +1319,40 @@ clownsEverywhere(
这样的规定也使得,函数参数与数组和对象的尾逗号规则,保持一致了。
## catch 语句的参数
目前,有一个[提案](https://github.com/tc39/proposal-optional-catch-binding),允许`try...catch`结构中的`catch`语句调用时不带有参数。这个提案跟参数有关,也放在这一章介绍。
传统的写法是`catch`语句必须带有参数,用来接收`try`代码块抛出的错误。
```javascript
try {
// ···
} catch (error) {
// ···
}
```
新的写法允许省略`catch`后面的参数,而不报错。
```javascript
try {
// ···
} catch {
// ···
}
```
新写法只在不需要错误实例的情况下有用,因此不及传统写法的用途广。
```javascript
let jsonData;
try {
jsonData = JSON.parse(str);
} catch {
jsonData = DEFAULT_DATA;
}
```
上面代码中,`JSON.parse`报错只有一种可能:解析失败。因此,可以不需要抛出的错误实例。