admin管理员组文章数量:1302405
I noticed today that looping of JavaScript object can be done using less curly brackets.
For instance, the normal way of doing things:
// The normal way
var foo = {
bar: 1
};
for(var key in foo) {
if(foo.hasOwnProperty(key)) {
console.log(foo[key]); // prints out 1
}
}
The alternative, by dropping out extra { ... }
and it still works:
// The alternative
var foo = {
bar: 1
};
for(var key in foo) if(foo.hasOwnProperty(key)) { // <-- see :)
console.log(foo[key]); // prints out 1
}
However, I am less sure why this is possible. So my question is: when curly brackets can be omitted? I got a downvote saying "too broad" so I try to emphasize that I am not looking a million use cases, short answer suits just fine which explains the basics.
Thanks in advance!
I noticed today that looping of JavaScript object can be done using less curly brackets.
For instance, the normal way of doing things:
// The normal way
var foo = {
bar: 1
};
for(var key in foo) {
if(foo.hasOwnProperty(key)) {
console.log(foo[key]); // prints out 1
}
}
The alternative, by dropping out extra { ... }
and it still works:
// The alternative
var foo = {
bar: 1
};
for(var key in foo) if(foo.hasOwnProperty(key)) { // <-- see :)
console.log(foo[key]); // prints out 1
}
However, I am less sure why this is possible. So my question is: when curly brackets can be omitted? I got a downvote saying "too broad" so I try to emphasize that I am not looking a million use cases, short answer suits just fine which explains the basics.
Thanks in advance!
Share edited Jul 21, 2014 at 19:09 Mauno Vähä asked Jul 21, 2014 at 18:51 Mauno VähäMauno Vähä 9,7883 gold badges35 silver badges55 bronze badges 4- 1 you can even omit the brackets in the for loop...pretty much anything that is just one line then you can remove the brackets – Benjamin Trent Commented Jul 21, 2014 at 18:54
- As a side note, it is advisable to always use curly brackets, as omitting them can lead to some very annoying bugs. Personally, I always use them. – forgivenson Commented Jul 21, 2014 at 19:18
- 1 @forgivenson good point, and note for downvoter. Leave a ment next time, I hate that when ppl just pass by and drops arrows. Can't read minds here. – Mauno Vähä Commented Jul 21, 2014 at 19:21
- @MaunoV.: me too dislike downvotes without ments – Sajad Karuthedath Commented Jul 21, 2014 at 19:24
4 Answers
Reset to default 7You can omit curly braces anytime there is only one statement.
for (var x in obj) {
console.log(x);
}
can bee
for (var x in obj)
console.log(x);
but if you had
for (var x in obj) {
console.log(x);
console.log(x);
}
it would need the curly braces.
This is somewhat inconsistent. For example, it works for for
, while
and if
, but not for try
or catch
. For consistency, I always use braces.
After a if
or loop (for
, while
) you can ommit the {
if you have only one statement after it. So
if (condition)
someFunction();
equals to
if (condition) {
someFunction();
}
And
if (condition) a(); b();
equals to
if (condition) {
a();
}
b();
You can even nest that:
if (condition)
while(condition2)
if (condition3)
someFunction();
For readibility it's best (read: I prefer) to always use braces (also less prone to make programming-errors).
As long it's one instruction you can omit curly brackets.
for(var key in foo)
if(foo.hasOwnProperty(key))
console.log(foo[key]); // prints out 1
for(var key in foo)
if(foo.hasOwnProperty(key))
for(var i=0; i<=10; i++) {
console.log(foo[key]); // prints out 1
console.log(foo[key]); // prints out 1
console.log(foo[key]); // prints out 1
}
As far as I know there is only one case where you can omit the curly braces and that is when you only have one statement after the condition
if (condition) statement;
in your example you have omitted the curly braces from the first condition and but kept them in the second
if (condition)
if (condition) { statement; }
[Note] It is possible to omit semicolons but it is considered bad programming practice.
本文标签: JavaScript When we can omit curly bracketsStack Overflow
版权声明:本文标题:JavaScript: When we can omit curly brackets? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741696701a2393044.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论