-
Notifications
You must be signed in to change notification settings - Fork 0
JS Coding Standard
Lin Liu edited this page Sep 12, 2013
·
1 revision
统一用 utf-8 使用linux换行符,如果用windows记事本打开代码变一陀说明成功了
长度不超过 80 个字符。如超过换行空双倍缩进(8个空格)
好
if (looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooog ||
loooooooooooooooooooooooooooooooooooooooooooooooooooooooooong2
) {
}
缩进使用4个 空格。禁用 tab
好
if (foo) {
}
坏
if (foo)
{
}
不允许一行判断,一律换行
坏
if (foo) return;
好
if (foo) {
return;
}
##命名约定
- 常量 UPPERCASE_WORD 名词
- 变量 camelName 名词
- 类名 CamelName 名词
- 函数名 camelName 动词
单引号优先原则,只有需要多层嵌套和json字面量时才考虑用双引号
好
var foo = 'hello';
var letter = "I'm char1ee";
或
var letter = 'I\'m char1ee'
坏
var foo = "hello";
不要养成不写分号的坏习惯, 只在函数声明、if 判断 for while try等}后可以省略引号 好
var foo = 1;
var bar = 2;
function hello() {
}
if (condition) {
}
逗号后空一格
好
var a, b, c;
function (a, b, c) {
}
坏
var a,b,c;
var a , b , c;
好
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'
}
首先,变量在使用前必须声明。
对于单 var 模式和多 var 模式,不做强行约定,但同一个文件里,风格必须一致。
配合 .jshintrc 做机器校验