Javascript node.js 应用程序的编码风格指南?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5495984/
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
Coding Style Guide for node.js apps?
提问by alienhard
Is there a (or several) coding style guide for node.js? If not, what are the emerging stylesused by the top open-source node projects?
node.js 是否有(或多个)编码风格指南?如果没有,顶级开源节点项目使用的新兴风格是什么?
I'm looking for a guide (or several guides) along the lines of PEP 8, the canonical Coding Style Guide for Python. I've seen various JavaScript guides not worth linking here (mostly old and targeted at client-side JavaScript). I found one interesting node.js style guide.
我正在寻找符合PEP 8(Python 规范编码风格指南)的指南(或多个指南)。我见过各种不值得在此处链接的 JavaScript 指南(大部分是旧的并且针对客户端 JavaScript)。我发现了一个有趣的 node.js风格指南。
A coding style guide, or coding conventions, should include (but is not limited to):
编码风格指南或编码约定应包括(但不限于):
- Code layout: indentation (2 spaces, 4 spaces, tabs, ...), newlines, line breaks, etc.
- Whitespace, e.g., "function (arg)" vs. "function(arg)"
- Semicolon or no semicolon, var declaration, ...
- Naming, e.g., do_this() vs. doThis(), var_name vs. varName, ...
- node.js and JavaScript idioms, e.g., == vs. ===, callback's first arg is an error object, ...
- Comments and documentation
- Accompanying tools, like lint checker, unit test framework, ...
- 代码布局:缩进(2 个空格、4 个空格、制表符、...)、换行符、换行符等。
- 空格,例如,“function (arg)”与“function(arg)”
- 分号或无分号,var 声明,...
- 命名,例如,do_this() 与 doThis(),var_name 与 varName,...
- node.js 和 JavaScript 习惯用法,例如 == 与 ===,回调的第一个 arg 是一个错误对象,...
- 评论和文档
- 附带工具,如 lint 检查器、单元测试框架、...
This topic obviously is highly subjective, but I think it's an important step of a community to establish a common and widely accepted coding style in the process of getting mature. Also, it's not all just about taste. In particular, rules like "use === instead of ==" have a direct influence on code quality.
这个话题显然是非常主观的,但我认为这是一个社区在逐渐成熟的过程中建立一个共同的、被广泛接受的编码风格的重要一步。此外,这不仅仅是味道。特别是,像“使用 === 而不是 ==”这样的规则对代码质量有直接影响。
采纳答案by chriso
I'd review the coding standards checked by JSLintor look at the author of NPM(Isaac Shlueter's) coding standards.
我会查看JSLint检查的编码标准或查看NPM(Isaac Shlueter's) coding Standards 的作者。
You could also look at the style used by notable Node.JS coders:
您还可以查看著名的 Node.JS 编码人员使用的样式:
- TJ Holowaychuk
- Isaac Shlueter
- Tim Caswell
- Jeremy Ashkenas
- Felix Geisend?rfer
- Charlie Robbins
- Marak Squires
- Aaron Heckmann
- Guillermo Rauch
- Mikeal Rogers
- Ryan Dahl+ you could look at the actual Node.JS codebase
- TJ霍洛韦查克
- 艾萨克·施卢特
- 蒂姆·卡斯威尔
- 杰里米·阿什肯纳斯
- Felix Geisend?rfer
- 查理罗宾斯
- 马拉克乡绅
- 亚伦·赫克曼
- 吉列尔莫·劳赫
- 麦克尔·罗杰斯
- Ryan Dahl+ 你可以看看实际的 Node.JS 代码库
I'll throw minein there for good measure ;)
我会把我的扔在那里好好衡量;)
Edit: Suggestions from @alienhard
编辑:来自@alienhard 的建议
IMO there's a few golden rules you should follow:
IMO 有一些您应该遵循的黄金法则:
- Never use
with
oreval
- Use
===
over==
- Always declare your variables with
var
in the appropriate scope - don't fallback to the global scope - Wrap your app in a closure
(function(){})()
if you plan on releasing code that runs server-side as well as in the browser - Callbacks should take
err
as the first argument and if they themselves take a callback as an argument, it should be last, e.g.callback(err, param1, param2, callback)
- 切勿使用
with
或eval
- 使用
===
过==
- 始终
var
在适当的范围内声明变量- 不要回退到全局范围 (function(){})()
如果您计划发布在服务器端和浏览器中运行的代码,请将您的应用程序包装在一个闭包中- 回调应该
err
作为第一个参数,如果他们自己将回调作为参数,它应该是最后一个,例如callback(err, param1, param2, callback)
Indentation, spacing between braces and keywords and semicolon placement are all a matter of preference.
缩进、大括号和关键字之间的间距以及分号位置都是偏好问题。
回答by mightyiam
回答by yojimbo87
You can learn a lot of good coding style practices from client side oriented JavaScript guides (most of them apply also to node.js in general since the difference between client and server side is mostly in libraries and not in language itself). For example JavaScript Patternsbook dedicates to this topic some parts of the Chapter 2. Also Douglas Crockford's website, bookand videosare a must see materials in order to adopt JavaScript specific coding styles and best practices I would say.
您可以从面向客户端的 JavaScript 指南中学到很多好的编码风格实践(其中大多数也适用于一般的 node.js,因为客户端和服务器端之间的区别主要在于库而不是语言本身)。例如,JavaScript Patterns一书在第 2 章的某些部分专门讨论了这个主题。此外,Douglas Crockford 的网站、书籍和视频是必须查看的材料,以便采用 JavaScript 特定的编码风格和我想说的最佳实践。
回答by Daniel Yankowsky
When using node from the terminal, it's useful for your source code to use spaces for indentation. Otherwise, the "error here" caret won't line up.
从终端使用 node 时,源代码使用空格进行缩进很有用。否则,“此处的错误”插入符号将不会排列。
With tabs:
带标签:
var preps = files.map(function(f) {
^
TypeError: Cannot call method 'map' of null
With spaces:
带空格:
var preps = files.map(function(f) {
^
TypeError: Cannot call method 'map' of null
This might be a Mac only issue, but I would suspect not.
这可能只是 Mac 的问题,但我怀疑不是。
回答by alienhard
It has been a while since I asked this question... and in the meantime I've found this excellent JavaScript guide:
我问这个问题已经有一段时间了……与此同时,我发现了这个优秀的 JavaScript 指南:
Principles of Writing Consistent, Idiomatic JavaScript
编写一致的、惯用的 JavaScript 的原则
回答by Dror
Airbnb has a quite good Javascript style guide https://github.com/airbnb/javascript
Airbnb 有一个相当不错的 Javascript 风格指南 https://github.com/airbnb/javascript
回答by Quang Van
For Coffee-Script, where bad indents means compilation errors
对于 Coffee-Script,错误的缩进意味着编译错误
use
用
:set tabstop=2
:set shiftwidth=2
:set expandtab
popular coffee projects, zombie
, brunch
uses this setup for indentations.
流行的咖啡项目zombie
,brunch
使用此设置的压痕。
Edit:
编辑:
Actually, just use this! https://github.com/paulmillr/code-style-guides(one of the main contributors to brunch
)
其实就用这个吧!https://github.com/paulmillr/code-style-guides(主要贡献者之一brunch
)