javascript if语句的简洁语法不带大括号 [英] Concise syntax for javascript if statements without curly brackets

查看:137
本文介绍了javascript if语句的简洁语法不带大括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,务实地,对于所要查找的内容,我有一个快速而肮脏的答案这里.但是为什么不使用这个好主意呢?为什么找不到有关它的任何正式文档?它不是规范和标准的一部分吗?是否得到广泛支持?仅仅是因为缩小会破坏使用该语法的代码吗?

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 块的内容?它是基于缩进的吗?如果是这样,那将很有趣.

If you could point me to more comprehensive 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.

另一方面,PHP中的 if 语句是否与此语法类似?我可以保证已经看到它们在这里和那里被使用了,但是我找不到任何可用的示例.我只是疯了而实际上在PHP中并不存在,还是可以在PHP中使用这些 if 类型的块?这样的 if 块在JS和PHP中是否也支持 else ?

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();

谢谢

推荐答案

但是为什么不使用这个好主意呢?

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?

当然是,请参见§ 12.5-The if声明§ 12-声明在规格中. if 的主体是 Statement .一种声明 Block (

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?

普遍.

仅仅是因为缩小可以使用该语法破坏代码吗?

Is it just because minification could break code using that syntax?

一个好的Minifier不会破坏该语法.(实际上,一个好的Minifier会充分利用它.)

A good minifier won't break that syntax. (A good minifier will make use of it, in fact.)

什么定义了if块的内容?它是基于缩进的吗?

What defines the contents of the if block? Is it indentation based?

if 语句的主体仅由其后的语句组成,缩进在JavaScript中没有任何意义.因此,所有这些都是等效的:

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();

在上面,仅 调用 bar 的条件是 foo ;无论如何,都会调用 charlie .

In the above, only the call to bar is conditional on foo; charlie is called regardless.

这就是为什么我们有 Block Statement 的原因,该语句引入了将被视为一个单元的语句列表(一个块,您可能会说:-)):

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. :-)

另一方面,PHP中的 if 语句是否与此语法类似?

On another note, is there something similar to this syntax for if statements in PHP?

我不是一个高大的PHP负责人,但它看起来完全一样,定义在控制结构- if .有带和不带 {} 的示例.(这里也将介绍另外一种不同的替代语法.)

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.)

在JS和PHP中,这样的 if 块是否也支持 else ?

是的, if 是否支持 else 带有和不带有块.

Yes, if supports else both with and without blocks.

这篇关于javascript if语句的简洁语法不带大括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆