javascript 中的正确缩进
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18841450/
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
Proper indentation in javascript
提问by
I'm fairly new to coding what are some good rules of thumb to make my code easy to read to me and others. When do I indent where should my curly braces be? If you have any resources for websites that are good tutorials for this please link me. I've finished codeacademy and a lot of other starter resources for js. I still seem to be struggling to figure out how to make my code look pretty and readable. Thanks for everybody's help and suggestions in advance!
我对编码什么是一些很好的经验法则很陌生,以使我的代码易于我和其他人阅读。我什么时候应该缩进花括号的位置?如果您有任何关于此的良好教程的网站资源,请链接我。我已经完成了 codeacademy 和许多其他 js 入门资源。我似乎仍在努力弄清楚如何使我的代码看起来漂亮和可读。提前感谢大家的帮助和建议!
回答by james emanon
Here you go: And like TGH says, there is no "right way", only the way you and your collegues agree on. As long as its consistent and you all agree, have at it.
给你:正如 TGH 所说,没有“正确的方式”,只有你和你的同事同意的方式。只要它一致并且你们都同意,就可以了。
this will help with prettying up your code: http://jsbeautifier.org/
这将有助于修饰您的代码:http: //jsbeautifier.org/
the following will help you with some standards.
以下将帮助您了解一些标准。
https://github.com/v0lkan/o2.js/blob/master/CONVENTIONS.md
https://github.com/v0lkan/o2.js/blob/master/CONVENTIONS.md
https://github.com/rwaldron/idiomatic.js/
https://github.com/rwaldron/idiomatic.js/
http://javascript.crockford.com/code.html
http://javascript.crockford.com/code.html
http://contribute.jquery.org/style-guide/js/
http://contribute.jquery.org/style-guide/js/
http://addyosmani.com/blog/javascript-style-guides-and-beautifiers/
http://addyosmani.com/blog/javascript-style-guides-and-beautifiers/
回答by Jonathan Lonowski
Coding style is generally decided on per-project. For example:
编码风格通常取决于每个项目。例如:
- jQuery uses tabs
- Bootstrap uses 2 spaces, no semicolons
- jQuery使用标签
- Bootstrap使用 2 个空格,没有分号
The one caveat is with Automatic Semicolon Insertion, primarily regarding a few "restricted productions" which do not allow line-breaks.
一个警告是自动分号插入,主要是关于一些不允许换行的“受限作品”。
return // returns `undefined`
{ // becomes a block rather than an `Object`
key: "value" // and a label rather than a property
};
Though, this brace style is safe in most other cases:
不过,这种大括号样式在大多数其他情况下是安全的:
function foo(bar)
{
console.log(foo, bar);
}
回答by TGH
There are no special rules for JavaScript since it's not a white space sensitive language. It comes down to your own personal preference, so there is really no right or wrong. Just pick one convention and use it consistently.
JavaScript 没有特殊规则,因为它不是对空格敏感的语言。这归结为你自己的个人喜好,所以真的没有对或错。只需选择一种约定并始终如一地使用它。