-
Notifications
You must be signed in to change notification settings - Fork 318
JavaScript 编码风格
lepture edited this page Apr 16, 2012
·
18 revisions
首先必须通过 arale/tools/gjslint 普通模式校验。 通不过的,不允许提交到 Git 库。
下面是一些常用注意点:
##长度不超过 80 字符
别小看这一条规则,如果严格去遵循,你会发现代码变清晰了。而且,你一定能做到的。
参考:
- pep8 为 79 个字符
- npm 为 80 个字符
- google 为 80 个字符
##缩进 4 空格
遵循大部分人的习惯,严禁用 tab 或 2 空格。
参考:
- npm 为 2 空格
- pep8 为 4 空格
- google 为 2 空格(gjslint没规定)
- 大部分前端工程师习惯 4 空格
Vim 设置 tab 为 4 空格:
set tabstop=4
set shiftwidth=4
set expandtab
##花括号
###花括号不换行
好
if (foo) {
}
坏
if (foo)
{
}
###一行判断可加也可不加花括号
好
if (foo) { …; }
也好
if (foo) …;
参考:
- npm 推荐一行判断不加花括号
- google 都可以
##命名约定
- 常量 UPPERCASE_WORD
- 变量 camelName
##空格
###操作符之间需要空格
好
var x = y + z
坏
var x=y+z
好
{
a: 'short',
looooongname: 'long'
}
坏
{
a: 'short',
looooongname: 'long'
}
(南伯继续完善中)