admin管理员组文章数量:1279043
So pragmatically, I've got a quick and dirty answer to what I'm looking for here. But why isn't using that a good idea? Why can't I find any formal documentation of it? Is it not part of the spec and standard? Is it not widely supported? Is it just because minification could break code using that syntax?
If you could point me to more prehensive docs of the feature, I'd appreciate that. What defines the contents of the if
block? Is it indentation based? If it was, that'd be interesting.
On another note, is there something similar to this syntax for if
statements in PHP? I can swear that I've seen them being used here and there, but I can't find any examples off hand. Am I just crazy and it actually doesn't exist in PHP, or can those types of if
blocks be used in PHP? Does such an if
block support having an else
as well, both in JS and PHP?
It seems that there's an indentation based one as well as a single-line based syntax as well. What can you tell me about the following?
if(condition) do_some_statement();
Thanks
So pragmatically, I've got a quick and dirty answer to what I'm looking for here. But why isn't using that a good idea? Why can't I find any formal documentation of it? Is it not part of the spec and standard? Is it not widely supported? Is it just because minification could break code using that syntax?
If you could point me to more prehensive docs of the feature, I'd appreciate that. What defines the contents of the if
block? Is it indentation based? If it was, that'd be interesting.
On another note, is there something similar to this syntax for if
statements in PHP? I can swear that I've seen them being used here and there, but I can't find any examples off hand. Am I just crazy and it actually doesn't exist in PHP, or can those types of if
blocks be used in PHP? Does such an if
block support having an else
as well, both in JS and PHP?
It seems that there's an indentation based one as well as a single-line based syntax as well. What can you tell me about the following?
if(condition) do_some_statement();
Thanks
Share Improve this question edited May 23, 2017 at 12:21 CommunityBot 11 silver badge asked Jul 27, 2012 at 12:39 haxxerzhaxxerz 9537 silver badges17 bronze badges 9- White space in JavaScript never has structural semantics like in Python. – Pointy Commented Jul 27, 2012 at 12:42
- Strongly remend reading even just a basic guide to both languages. – T.J. Crowder Commented Jul 27, 2012 at 13:01
- T.J. - Would crockford's The good parts qualify? – haxxerz Commented Jul 27, 2012 at 13:08
- And any corresponding book you'd remend for PHP? – haxxerz Commented Jul 27, 2012 at 13:08
- 1 @haxxerz: Oh, er, um, also I blog about JavaScript on occasion. – T.J. Crowder Commented Jul 27, 2012 at 13:20
5 Answers
Reset to default 9But why isn't using that a good idea?
Because it's hard to maintain.
Why can't I find any formal documentation of it? Is it not part of the spec and standard?
Of course it is, see §12.5 - The if
Statement and §12 - Statements in the spec. The body of an if
is a Statement. One kind of Statement is Block (§12.1), which allows a list of statements to be treated as one statement, but there are many other kinds of statements.
Is it not widely supported?
Universally.
Is it just because minification could break code using that syntax?
A good minifier won't break that syntax. (A good minifier will make use of it, in fact.)
What defines the contents of the if block? Is it indentation based?
The body of an if
statement consists only of the statement following it, indentation has no significance in JavaScript. So all of these are equivalent:
if (foo)
bar();
charlie();
if (foo) bar();
charlie();
if (foo)
bar(); charlie();
if (foo)
bar();
charlie();
In the above, only the call to bar
is conditional on foo
; charlie
is called regardless.
That's why we have Block, the Statement that introduces a list of statements to be treated as a unit (a block, you might say :-) ):
if (foo) {
bar();
}
charlie();
if (foo) { bar(); }
charlie();
if (foo) {
bar(); } charlie();
if (foo)
{ bar(); }
charlie();
Indentation is important for humans, though, so keeping consistent indentation is a good idea. The first example in each of the above is probably clearest (of the ones listed) for us mere mortals. :-)
On another note, is there something similar to this syntax for
if
statements in PHP?
I'm not a big PHP-head, but it looks identical, defined in Control Structures - if
. There are examples with and without {}
. (There's also a different, alternative syntax I won't go into here.)
Does such an
if
block support having anelse
as well, both in JS and PHP?
Yes, if
supports else
both with and without blocks.
javascript is not white space sensitive, meaning
if(condition) do_some_statement();
is the same as
if(condition)
do_some_statement();
that being said, omitting braces in a single line if statement is always frowned upon because it can lead to bugs if the if statement ever needs to be modified:
if(condition)
do_some_statement();
// someone adds another line here, without adding the braces
// now you've introduced a bug
also, is it really that hard to write { }
? :P
The statement following an if
is just that: a statement.
One of the possible forms a statement can take is that of a brace-enclosed group of statements.
Thus, the syntax of an if
statement is
if ( expression ) statement
Thus the reason that braces improve maintainability is that they provide an explicit boundary to the scope of influence of the if
control flow effect.
why isn't using that a good idea?
It is far to easy to add another statement and expect it to only fire if the if
passes
Why can't I find any formal documentation of it?
The MDN documentation:
Statement that is executed if condition evaluates to true. Can be any statement, including further nested if statements. To execute multiple statements, use a block statement ({ ... }) to group those statements.
ECMA-262 (page 89):
if ( Expression ) Statement
It seems that there's an indentation based one
No. Just an if, then a condition, then a statement. The statement can be formatted on the same line or the next line.
White space is not significant in JS.
It is standard, part of the spec (if-statement, other statements) and supported everywhere. Minification does not break it, because whitespaces have no semantics in JS - it even will enforce it for one-line-statements to save the two braces.
So, it is widely used (without problems!) as well. Sometimes it is considered bad because appending statements to the indented body without adding the braces can lead to problems. Also, it can lead to erratic behaviour when used in nested ifs:
if (false)
if (whatever)
;
else
alert("");
Would you have expected an alert? No, the else
belongs to the last if
.
Yet, you can use it unconcerned for one-line-statements that are sure not to get extended, like return;
.
本文标签: phpConcise syntax for javascript if statements without curly bracketsStack Overflow
版权声明:本文标题:php - Concise syntax for javascript if statements without curly brackets - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741233986a2362640.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论