* [22.1](https://github.com/yuche/javascript#22.1)?避免單字母命名。命名應具備描述性。
~~~
// bad
function q() {
// ...stuff...
}
// good
function query() {
// ..stuff..
}
~~~
* [22.2](https://github.com/yuche/javascript#22.2)?使用駝峰式命名對象、函數和實例。
~~~
// bad
const OBJEcttsssss = {};
const this_is_my_object = {};
function c() {}
// good
const thisIsMyObject = {};
function thisIsMyFunction() {}
~~~
* [22.3](https://github.com/yuche/javascript#22.3)?使用帕斯卡式命名構造函數或類。
~~~
// bad
function user(options) {
this.name = options.name;
}
const bad = new user({
name: 'nope',
});
// good
class User {
constructor(options) {
this.name = options.name;
}
}
const good = new User({
name: 'yup',
});
~~~
* [22.4](https://github.com/yuche/javascript#22.4)?使用下劃線?`_`?開頭命名私有屬性。
~~~
// bad
this.__firstName__ = 'Panda';
this.firstName_ = 'Panda';
// good
this._firstName = 'Panda';
~~~
* [22.5](https://github.com/yuche/javascript#22.5)?別保存?`this`?的引用。使用箭頭函數或 Function#bind。
~~~
// bad
function foo() {
const self = this;
return function() {
console.log(self);
};
}
// bad
function foo() {
const that = this;
return function() {
console.log(that);
};
}
// good
function foo() {
return () => {
console.log(this);
};
}
~~~
* [22.6](https://github.com/yuche/javascript#22.6)?如果你的文件只輸出一個類,那你的文件名必須和類名完全保持一致。
~~~
// file contents
class CheckBox {
// ...
}
export default CheckBox;
// in some other file
// bad
import CheckBox from './checkBox';
// bad
import CheckBox from './check_box';
// good
import CheckBox from './CheckBox';
~~~
* [22.7](https://github.com/yuche/javascript#22.7)?當你導出默認的函數時使用駝峰式命名。你的文件名必須和函數名完全保持一致。
~~~
function makeStyleGuide() {
}
export default makeStyleGuide;
~~~
* [22.8](https://github.com/yuche/javascript#22.8)?當你導出單例、函數庫、空對象時使用帕斯卡式命名。
~~~
const AirbnbStyleGuide = {
es6: {
}
};
export default AirbnbStyleGuide;
~~~
- 關于
- 1. 類型
- 2. 引用
- 3. 對象
- 4. 數組
- 5. 解構
- 6. 字符串
- 7. 函數
- 8. 箭頭函數
- 9. 構造函數
- 10. 模塊
- 11. Iterators & Generators
- 12. 屬性
- 13. 變量
- 14. 提升
- 15. 比較運算符 & 等號
- 16. 代碼塊
- 17. 注釋
- 18. 空白
- 19. 逗號
- 20. 分號
- 21. 類型轉換
- 22. 命名規則
- 23. 存取器
- 24. 事件
- 25. jQuery
- 26. ECMAScript 5 兼容性
- 27. ECMAScript 6 編碼規范
- 28. 測試
- 29. 性能
- 30. 資源
- 31. 使用人群
- 32. 翻譯
- 33. JavaScript 編碼規范說明
- 34. 一起來討論Javascript
- 35. Contributors
- 36. License