## 基本
* 縮進使用soft tab(4個空格)
* 單行長度不要超過80
* 語句結束需要添加分號
* 標準變量采用駝峰命名法
* 常量全大寫用下劃線連接
* 函數作用域下的所有變量盡量提到函數首部。
* 下列關鍵字后必須有大括號(即使代碼塊的內容只有一行):if, else,for, while, do, switch, try, catch, finally, with
### 1. 空格
#### 以下幾種情況不需要空格:
* 對象的屬性名后
* 前綴一元運算符后
* 后綴一元運算符前
* 函數調用括號前
* 無論是函數聲明還是函數表達式,'('前不要空格
* 數組的'['后和']'前
* 對象的'{'后和'}'前
* 運算符'('后和')'前
#### 以下幾種情況需要空格:
* 二元運算符前后
* 三元運算符'?:'前后
* 代碼塊'{'前
* 下列關鍵字前:else, while, catch, finally
* 下列關鍵字后:if, else, for, while, do, switch, case, try,catch, finally, with, return, typeof
* 單行注釋'//'后(若單行注釋和代碼同行,則'//'前也需要),多行注釋'*'后
* 對象的屬性值前
* for循環,分號后留有一個空格,前置條件如果有多個,逗號后留一個空格
* 無論是函數聲明還是函數表達式,'{'前一定要有空格
* 函數的參數之間
例如:
~~~
// not good
var a = {
b :1
};
// good
var a = {
b: 1
};
// not good
++ x;
y ++;
z = x?1:2;
// good
++x;
y++;
z = x ? 1 : 2;
// not good
var a = [ 1, 2 ];
// good
var a = [1, 2];
// not good
var a = ( 1+2 )*3;
// good
var a = (1 + 2) * 3;
// no space before '(', one space before '{', one space between function parameters
var doSomething = function(a, b, c) {
// do something
};
// no space before '('
doSomething(item);
// not good
for(i=0;i<6;i++){
x++;
}
// good
for (i = 0; i < 6; i++) {
x++;
}
~~~
### 2. 空行
#### 以下幾種情況需要空行:
* 變量聲明后(當變量聲明在代碼塊的最后一行時,則無需空行)
* 注釋前(當注釋在代碼塊的第一行時,則無需空行)
* 代碼塊后(在函數調用、數組、對象中則無需空行)
* 文件最后保留一個空行
~~~
// need blank line after variable declaration
var x = 1;
// not need blank line when variable declaration is last expression in the current block
if (x >= 1) {
var y = x + 1;
}
var a = 2;
// need blank line before line comment
a++;
function b() {
// not need blank line when comment is first line of block
return a;
}
// need blank line after blocks
for (var i = 0; i < 2; i++) {
if (true) {
return false;
}
continue;
}
var obj = {
foo: function() {
return 1;
},
bar: function() {
return 2;
}
};
// not need blank line when in argument list, array, object
func(
2,
function() {
a++;
},
3
);
var foo = [
2,
function() {
a++;
},
3
];
var foo = {
a: 2,
b: function() {
a++;
},
c: 3
};
~~~
### 3. 換行
#### 換行的地方,行末必須有','或者運算符;
#### 以下幾種情況不需要換行:
* 下列關鍵字后:else, catch, finally
* 代碼塊'{'前
以下幾種情況需要換行:
* 代碼塊'{'后和'}'前
* 變量賦值后
~~~
// not good
var a = {
b: 1
, c: 2
};
x = y
? 1 : 2;
// good
var a = {
b: 1,
c: 2
};
x = y ? 1 : 2;
x = y ?
1 : 2;
// no need line break with 'else', 'catch', 'finally'
if (condition) {
...
} else {
...
}
try {
...
} catch (e) {
...
} finally {
...
}
// not good
function test()
{
...
}
// good
function test() {
...
}
// not good
var a, foo = 7, b,
c, bar = 8;
// good
var a,
foo = 7,
b, c, bar = 8;
~~~
### 4. 單行注釋
* 雙斜線后,必須跟一個空格;
* 縮進與下一行代碼保持一致;
~~~
if (condition) {
// if you made it here, then all security checks passed
allowed();
}
~~~
### 5. 多行注釋
#### '*'后跟一個空格
### 6. 文檔注釋
[usejsdoc](http://yuri4ever.github.io/jsdoc/)
### 7. null和undefined
* 不要直接使用undefined進行變量判斷,使用typeof和字符串'undefined'對變量進行判斷
* 不要用null來判斷函數調用時有無傳參
~~~
// not good
if (person === undefined) {
...
}
// good
if (typeof person === 'undefined') {
...
}
// not good
function test(a, b) {
if (b === null) {
// not mean b is not supply
...
}
}
var a;
if (a === null) {
...
}
// good
var a = null;
if (a === null) {
...
}
~~~