From 7a85871d4a7e545e9774de8d6cc20950d68ff92c Mon Sep 17 00:00:00 2001 From: ruanyf Date: Mon, 14 Aug 2017 14:11:40 +0800 Subject: [PATCH] docs(functio): optional catch binding --- docs/function.md | 37 +++++++++++++++++++++++++++++++++++++ docs/reference.md | 1 + 2 files changed, 38 insertions(+) diff --git a/docs/function.md b/docs/function.md index ca1d532..85565dc 100644 --- a/docs/function.md +++ b/docs/function.md @@ -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`报错只有一种可能:解析失败。因此,可以不需要抛出的错误实例。 + diff --git a/docs/reference.md b/docs/reference.md index b266517..3a7fac5 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -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) ## 对象