-
Notifications
You must be signed in to change notification settings - Fork 318
JavaScript 编码风格
lifesinger edited this page Apr 29, 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) return;
##命名约定
- 常量 UPPERCASE_WORD
- 变量 camelName
- 类名 CamelName
好
var x = y + z
坏
var x=y+z
好
{
a: 'short',
looooongname: 'long'
}
坏
{
a : 'short',
looooongname: 'long'
}
建议用自然人的处理方法
{
a: 'a',
b: 'b',
c: 'c'
}
不建议使用 npm 风格的逗号与换行,即
{
a: 'a'
,b: 'b'
,c: 'c'
}
(继续完善中)