JavaScript 大括号是否换行?

发布于 2024-09-09 02:00:29 字数 321 浏览 6 评论 0原文

在工作中,我们在下一行放置大括号,但在家里,我做相反的事情。你更喜欢哪一个? (K&R 与 OTBS)

function something() {
    // ...
}

function something()
{
    // ...
}

许多 JavaScript 库似乎都使用 OTBS(一种真正的大括号样式)。我想遵循它们以与其他 JavaScript 项目保持一致,但是 K&R 风格看起来不是更具可读性吗?

注意:我们知道 JavaScript 中返回和大括号的问题,这始终是一个例外。然而,这只是单一案例。

At work, we place braces on the next line, but at home, I do the opposite. Which one do you prefer? (K&R vs OTBS)

function something() {
    // ...
}

function something()
{
    // ...
}

A lot of JavaScript libraries seem to use the OTBS (one true brace style). I'd like to follow them for consistence among other JavaScript projects, but doesn't K&R style look more readable?

Note: We know the problem with return and braces in JavaScript, that will always be an exception. However, that is only a single case.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

月竹挽风 2024-09-16 02:00:29

Douglas Crockford给出了选择 K&R 风格的理由1

我总是使用 K&R 风格,将 { 放在行尾而不是前面,因为它避免了 JavaScript return 中出现可怕的设计错误声明。

他所指的错误是 JavaScript 在以下两种情况下如何以不同的方式处理 return 语句:

return {
   'status': 'ok'
};

...并且:

return 
{
   'status': 'ok'
};

第一个将返回具有 status 属性的对象,而后者将由于分号插入而返回undefined


1 Douglas Crockford:JavaScript:优秀部分:风格(第 96 页) ) - ISBN: 978-0596517748。

Douglas Crockford gives a reason for choosing the K&R style1:

I always use the K&R style, putting the { at the end of a line instead of the front, because it avoids a horrible design blunder in JavaScript's return statement.

The blunder he is referring to is how JavaScript handles the return statement differently in the following two scenarios:

return {
   'status': 'ok'
};

... and:

return 
{
   'status': 'ok'
};

The first one will return an object with a status property, while the latter will return undefined because of semicolon insertion.


1 Douglas Crockford: JavaScript: The Good Parts: Style (page 96) - ISBN: 978-0596517748.

红颜悴 2024-09-16 02:00:29

这是一场你永远无法得到可用答案的圣战!只要坚持项目中其他人正在使用的任何东西,不要争论!

无论如何,我是 K&Rite 的一员。我发现 OTBS 在开放结构和以下陈述之间放置了很多视觉空间,而这两行通常密切相关,因此最好一起呈现,而无需插入几乎空白的行。我喜欢保留空白行来分隔相关语句块。

在无论如何都有大量空白的编码风格中,这可能相对不重要。但我个人看重简洁,这样我可以在屏幕上保留更多的程序内容。

我不认为将开括号放在与闭括号不同的列上是一个问题。仅从凹痕中仍然很容易看出块的形状。除非您使用悬挂缩进。不要那样做。但这完全是另一场圣战。

This is a Holy War to which you will never get a usable answer! Just stick with whatever everyone else in the project is using and don't argue!

For what it's worth, I'm a K&Rite. I find the OTBS puts a lot of visual space between an opening structure and the following statement, when those two lines are often strongly related so would be better presented together, without an intervening almost-blank line. I like to keep my blank lines reserved for separating blocks of related statements.

In a coding style which a lot of whitespace anyway, this may be relatively unimportant. But personally I value terseness, so I can keep more of the program on-screen.

I don't buy that having the open-brace on a different column to the close-brace is an issue. It's still easy to see the block shape just from the indents. Unless you're using hanging indents. Don't do that. But that's another Holy War entirely.

紧拥背影 2024-09-16 02:00:29

我遵循 Douglas Crockford 的 JavaScript 编码约定,该约定的灵感来自于 Sun 的 Java 风格指南。

这是它的链接:http://javascript.crockford.com/code.html

I follow Douglas Crockford's JavaScript coding convention, which was inspired by Sun's Java style guidelines.

Here's a link to it: http://javascript.crockford.com/code.html

笙痞 2024-09-16 02:00:29

在我看来,这取决于还有谁将使用您的代码。如果您在 C# 团队中工作并分担很多责任,请将其置于新的路线上,并避免随之而来的不可避免的争吵。如果您与很多 PHP(或老 JS 程序员)一起工作,出于完全相同的原因将其放在第一行。

但是,如果您正在寻找更权威的内容,Douglas Crockford 表示,左大括号应该 始终位于顶行。如果我没记错的话,他的推理是它使其与语言的其他部分保持一致。基本上,因为这是有效但(可能)不正确的代码:

function myFunc() 
{
    return 
    {
      ok: true
    };
} 

...您应该普遍避免将开括号放在新行上。为什么?因为编程风格不应该导致语法歧义。

上面的示例代码是有效的,因为它在语法上完全正确,并且如果您编写此代码,则不会引发异常。但是,它不会返回对象文字 {ok:true},而是返回 undefined 并且无法访问其下面的代码。将左大括号放在一行,它将返回您可能期望的对象文字。

问题是,你认为这个论点足够有说服力吗?

In my opinion, it depends on who else will be working with your code. If you work on a C# team and share a lot of responsibilities, put it on a new line and avoid the inevitable bickering that would otherwise follow. If you work with a lot of PHP (or older JS programmers), put it on the first line for the exact same reason.

But if you're looking for something more authoritative, Douglas Crockford says that the opening brace should always be on the top line. His reasoning, if I remember correctly, is that it makes it consistent with the rest of the language. Basically, because this is valid but (probably) incorrect code:

function myFunc() 
{
    return 
    {
      ok: true
    };
} 

...you should universally avoid putting open-braces on a new line. Why? Because programming style should not lead to syntactic ambiguity.

The sample code above is valid because it is completely syntactically correct and no exceptions will be raised if you write this. However, instead of returning an object literal, {ok:true}, it will return undefined and the code below it will not be reached. Put the opening braces up one line and it will return the object literal you were, perhaps, expecting.

The question is, do you find that argument compelling enough?

夕色琉璃 2024-09-16 02:00:29

两者都不比另一者更好。只需选择一种并持续使用即可。

Neither one is better than the other. Just choose one and use it consistently.

墨洒年华 2024-09-16 02:00:29

都是主观的。有些稍微好一些,但差异可以忽略不计。 要做的最重要的事情是在所有代码中保持一致。

就我个人而言,我更喜欢隐藏式风格,带有 4 个空格的“真实”选项卡。

function a() {
    if (b) {
        do;
    } else {
        do2;
    }
}

All subjective. Some are slightly better but the difference is negligible. The most important thing to do is stay consistent across all your code.

Personally, I prefer the tucked in style, with 4 space 'real' tabs.

function a() {
    if (b) {
        do;
    } else {
        do2;
    }
}
简单气质女生网名 2024-09-16 02:00:29

我更喜欢它们在同一行,但主要是因为我经常传递匿名函数作为参数......它节省了一些空间并使函数定义看起来不那么刺耳。当然,这只是一个观点,我同意Bytecode Ninja的观点,一致性才是最重要的。

I prefer them on the same line, but mostly because I pass anonymous functions as arguments a lot...it saves some space and makes the function definition look less jarring. Of course, this is just an opinion, and I agree with Bytecode Ninja that consistency is the most important thing.

守望孤独 2024-09-16 02:00:29

正如许多答案中所述,最重要的是找到您(和/或您的队友,如果适用)坚持的风格。就我个人而言,我更喜欢同一行,因为我发现,如果您正在使用闭包和嵌套函数,则在新行上放置大括号可能会导致大量接近空白的行,这使得代码对我来说可读性较差(尽管,可能不适合大多数人...)

As stated in many answers, its mostly important that you find a style that you (and/or your team mates, if applicable) stick to. Personally I prefer same line as I've found that putting curly braces on the new line can lead to lots of near blank lines if you're working with closures and nested functions, which makes code less readable for me (although, probably not for most people...)

≈。彩虹 2024-09-16 02:00:29

出于与上述相同的原因,我更喜欢 K&R 方法。它看起来更紧凑,两条相关的线组合在一起。

I prefer the K&R method for the same reasons listed above. It looks more compact, and the two relevant lines are grouped together.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文