最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can curlies be omitted for try andor catch blocks, as for if and else ones? - Stack Overflow

programmeradmin4浏览0评论
if (foo) {
  bar;
}

can be shortened to

if(foo) bar;

since it's only one statement in the block.

I'm wondering if the same applies to try/catch... I don't like extra cruft in my code.

if (foo) {
  bar;
}

can be shortened to

if(foo) bar;

since it's only one statement in the block.

I'm wondering if the same applies to try/catch... I don't like extra cruft in my code.

Share Improve this question edited Jan 24, 2016 at 5:51 rink.attendant.6 46.2k64 gold badges110 silver badges157 bronze badges asked Nov 14, 2012 at 23:55 wwaawawwwaawaw 7,13710 gold badges35 silver badges42 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 21

According to ECMAScript 5, a block is required, which means you need the curly braces.

https://es5.github.io/#x12.14

TryStatement :

try Block Catch

try Block Finally

try Block Catch Finally

Catch :

catch ( Identifier ) Block

Finally :

finally Block

https://es5.github.io/#x12.1

Block :

{ StatementList opt }

StatementList :

Statement

StatementList Statement

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论