admin管理员组文章数量:1398811
Using google closure piler I get the following warning message:
WARNING - If this if/for/while really shouldnt have a body, use {}.
Here's what the documentation says:
This warning means that you have a semi-colon immediately following an if, for, or while statement. For example:
// Produces JSC_SUSPICIOUS_SEMICOLON warning: if (true); else alert('no');
Is this just a code convention thing or is {}
better than ;
for some reason?
Using google closure piler I get the following warning message:
WARNING - If this if/for/while really shouldnt have a body, use {}.
Here's what the documentation says:
This warning means that you have a semi-colon immediately following an if, for, or while statement. For example:
// Produces JSC_SUSPICIOUS_SEMICOLON warning: if (true); else alert('no');
Is this just a code convention thing or is {}
better than ;
for some reason?
- Why wouldn't you just test for the opposite? – Evan Davis Commented Jul 22, 2013 at 15:17
- @Mathletics what do you mean? – tim peterson Commented Jul 22, 2013 at 15:18
- 2 It's more clear to me that the if/for/while has an empty body if I read {} – Ye Liu Commented Jul 22, 2013 at 15:18
-
3
Why wouldn't you write
if (!condition) alert('no')
instead of the empty body? – Evan Davis Commented Jul 22, 2013 at 15:20
1 Answer
Reset to default 9This guards against things like:
if(condition);
{
// oops, I really meant for this to be conditional.
// now it always executes, regardless of condition.
}
The error raised forces you to explicitly put the empty {}
before the ;
so it's clear that that's what you intended.
本文标签:
版权声明:本文标题:javascript - if, for, or while with an empty body, use empty braces instead of a semi-colon - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744106332a2591081.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论