Skip to content

Commit 4bb8b17

Browse files
committed
docs(function): add ES2019 proposals
1 parent ac5a037 commit 4bb8b17

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

docs/function.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,3 +1312,52 @@ clownsEverywhere(
13121312
```
13131313

13141314
这样的规定也使得,函数参数与数组和对象的尾逗号规则,保持一致了。
1315+
1316+
## Function.prototype.toString()
1317+
1318+
[ES2019](https://github.com/tc39/Function-prototype-toString-revision) 对函数实例的`toString()`方法做出了修改。
1319+
1320+
`toString()`方法返回函数代码本身,以前会省略注释和空格。
1321+
1322+
```javascript
1323+
function /* foo comment */ foo () {}
1324+
1325+
foo.toString()
1326+
// function foo() {}
1327+
```
1328+
1329+
上面代码中,函数`foo`的原始代码包含注释,函数名`foo`和圆括号之间有空格,但是`toString()`方法都把它们省略了。
1330+
1331+
修改后的`toString()`方法,明确要求返回一模一样的原始代码。
1332+
1333+
```javascript
1334+
function /* foo comment */ foo () {}
1335+
1336+
foo.toString()
1337+
// "function /* foo comment */ foo () {}"
1338+
```
1339+
1340+
## catch 命令的参数省略
1341+
1342+
JavaScript 语言的`try...catch`结构,以前明确要求`catch`命令后面必须跟参数,接受`try`代码块抛出的错误对象。
1343+
1344+
```javascript
1345+
try {
1346+
// ...
1347+
} catch (err) {
1348+
// 处理错误
1349+
}
1350+
```
1351+
1352+
上面代码中,`catch`命令后面带有参数`err`
1353+
1354+
很多时候,`catch`代码块可能用不到这个参数。但是,为了保证语法正确,还是必须写。[ES2019](https://github.com/tc39/proposal-optional-catch-binding) 做出了改变,允许`catch`语句省略参数。
1355+
1356+
```javascript
1357+
try {
1358+
// ...
1359+
} catch {
1360+
// ...
1361+
}
1362+
```
1363+

0 commit comments

Comments
 (0)