mirror of
https://github.com/ruanyf/es6tutorial.git
synced 2025-05-24 10:22:23 +00:00
docs(functio): optional catch binding
This commit is contained in:
parent
fb2930d272
commit
7a85871d4a
@ -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`报错只有一种可能:解析失败。因此,可以不需要抛出的错误实例。
|
||||
|
||||
|
@ -90,6 +90,7 @@
|
||||
- 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指向,必须非常小心
|
||||
- Mark McDonnell, [Understanding recursion in functional JavaScript programming](http://www.integralist.co.uk/posts/js-recursion.html): 如何自己实现尾递归优化
|
||||
- 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/): 使用参数默认值时,不能在函数内部显式开启严格模式
|
||||
- Axel Rauschmayer, [ES proposal: optional catch binding](http://2ality.com/2017/08/optional-catch-binding.html)
|
||||
|
||||
## 对象
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user