JavaScript 大括号是否在新行上?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3218756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript braces on new line or not?
提问by Tower
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)
在工作中,我们将大括号放在下一行,但在家里,我做相反的事情。你更倾向哪个?(K&R 与 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?
许多 JavaScript 库似乎都使用 OTBS(一种真正的大括号样式)。为了与其他 JavaScript 项目保持一致,我想遵循它们,但是 K&R 样式看起来不是更具可读性吗?
Note: We know the problem with return and braces in JavaScript, that will always be an exception. However, that is only a single case.
注意:我们知道 JavaScript 中返回和大括号的问题,这将始终是一个例外。然而,这只是一个案例。
采纳答案by bobince
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.
就其价值而言,我是 K&Rite。我发现 OTBS 在开放结构和以下语句之间放置了很多视觉空间,当这两条线通常密切相关时,因此最好一起呈现,而没有中间几乎空白的线。我喜欢保留我的空行来分隔相关语句块。
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.
我不认为在不同的列上放置开放式支撑与封闭式支撑是一个问题。仅从缩进处看块形状仍然很容易。除非您使用悬挂缩进。不要那样做。但这完全是另一场圣战。
回答by Daniel Vassallo
Douglas Crockfordgives a reason for choosing the K&R style1:
Douglas Crockford给出了选择 K&R 款式1的理由:
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'sreturnstatement.
我总是使用 K&R 样式,将 放在
{行尾而不是行首,因为它避免了 JavaScriptreturn语句中可怕的设计错误。
The blunder he is referring to is how JavaScript handles the returnstatement differently in the following two scenarios:
他所指的错误是 JavaScriptreturn在以下两种情况下如何以不同的方式处理语句:
return {
'status': 'ok'
};
... and:
... 和:
return
{
'status': 'ok'
};
The first one will return an object with a statusproperty, while the latter will return undefinedbecause of semicolon insertion.
第一个将返回一个具有status属性的对象,而后者将undefined由于分号插入而返回。
1Douglas Crockford: JavaScript: The Good Parts: Style (page 96) - ISBN: 978-0596517748.
1 Douglas Crockford:JavaScript:好的部分:样式(第 96 页)- ISBN:978-0596517748。
回答by dalton
I follow Douglas Crockford's JavaScript coding convention, which was inspired by Sun's Java style guidelines.
我遵循 Douglas Crockford 的 JavaScript 编码约定,其灵感来自 Sun 的 Java 风格指南。
Here's a link to it: http://javascript.crockford.com/code.html
回答by Andrew
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.
在我看来,这取决于还有谁将使用您的代码。如果你在一个 C# 团队工作并分担很多责任,把它放在一个新的线上,避免不可避免的争吵。如果您与很多 PHP(或较老的 JS 程序员)一起工作,出于完全相同的原因,请将其放在第一行。
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 notlead 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 undefinedand 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.
上面的示例代码是有效的,因为它在语法上是完全正确的,如果您编写此代码,则不会引发任何异常。但是,{ok:true}它不会返回对象字面量,而是会返回undefined并且无法访问它下面的代码。将左大括号放在一行上,它将返回您可能期望的对象字面量。
The question is, do you find that argument compelling enough?
问题是,你觉得这个论点足够令人信服吗?
回答by Behrang Saeedzadeh
Neither one is better than the other. Just choose one and use it consistently.
没有一个比另一个更好。只需选择一个并始终如一地使用它。
回答by Delan Azabani
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.
就个人而言,我更喜欢带有 4 个空格“真实”标签的折叠式风格。
function a() {
if (b) {
do;
} else {
do2;
}
}
回答by Faisal
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.
我更喜欢它们在同一行,但主要是因为我将匿名函数作为参数传递了很多......它节省了一些空间并使函数定义看起来不那么刺耳。当然,这只是一种意见,我同意字节码忍者的观点,一致性是最重要的。
回答by Jake
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...)
正如许多答案中所述,最重要的是找到一种您(和/或您的队友,如果适用)坚持的风格。就我个人而言,我更喜欢同一行,因为我发现如果您正在使用闭包和嵌套函数,将花括号放在新行上会导致很多接近空白的行,这使我的代码可读性降低(尽管,可能不是大多数人...)
回答by computersaurus
I prefer the K&R method for the same reasons listed above. It looks more compact, and the two relevant lines are grouped together.
出于与上述相同的原因,我更喜欢 K&R 方法。它看起来更紧凑,两条相关的线组合在一起。

