- 在命名对象、函数和实例时使用驼峰命名法(camelCase) ```javascript // bad const OBJEcttsssss = {}; const this_is_my_object = {}; function c() {}
// good const thisIsMyObject = {}; function thisIsMyFunction() {}
2. 只有在命名构造器或者类的时候才用帕斯卡拼命名法(PascalCase)
```javascript
// 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',
});
- 文件名应该和默认导出的名称完全匹配 ```javascript // file 1 contents class CheckBox { // … } export default CheckBox;
// file 2 contents export default function fortyTwo() { return 42; }
// file 3 contents export default function insideDirectory() {}
// in some other file // bad import CheckBox from ‘./checkBox’; // PascalCase import/export, camelCase filename import FortyTwo from ‘./FortyTwo’; // PascalCase import/filename, camelCase export import InsideDirectory from ‘./InsideDirectory’; // PascalCase import/filename, camelCase export
// bad import CheckBox from ‘./check_box’; // PascalCase import/export, snake_case filename import forty_two from ‘./forty_two’; // snake_case import/filename, camelCase export import inside_directory from ‘./inside_directory’; // snake_case import, camelCase export import index from ‘./inside_directory/index’; // requiring the index file explicitly import insideDirectory from ‘./insideDirectory/index’; // requiring the index file explicitly
// good import CheckBox from ‘./CheckBox’; // PascalCase export/import/filename import fortyTwo from ‘./fortyTwo’; // camelCase export/import/filename import insideDirectory from ‘./insideDirectory’; // camelCase export/import/directory name/implicit “index” // ^ supports both insideDirectory.js and insideDirectory/index.js
4. 当你导出默认函数时使用驼峰命名法。 你的文件名应该和方法名相同
```javascript
function makeStyleGuide() {
// ...
}
export default makeStyleGuide;
- 当你导出一个构造器 / 类 / 单例 / 函数库 / 暴露的对象时应该使用帕斯卡命名法 ```javascript const AirbnbStyleGuide = { es6: { }, };
export default AirbnbStyleGuide;
6. 缩略词和缩写都必须是全部大写或者全部小写
```javascript
// bad
import SmsContainer from './containers/SmsContainer';
// bad
const HttpRequests = [
// ...
];
// good
import SMSContainer from './containers/SMSContainer';
// good
const HTTPRequests = [
// ...
];
// also good
const httpRequests = [
// ...
];
// best
import TextMessageContainer from './containers/TextMessageContainer';
// best
const requests = [
// ...
];
- 你可以大写一个常量,如果它:(1)被导出,(2)使用 const 定义(不能被重新赋值),(3)程序员可以信任它(以及其嵌套的属性)是不变的 ```javascript // bad const PRIVATE_VARIABLE = ‘不应该在文件中不必要地大写’;
// bad export const THING_TO_BE_CHANGED = ‘会更改的值、显然不应该大写’;
// bad export let REASSIGNABLE_VARIABLE = ‘不要在大写变量中使用let’;
// —-
// 允许,但是不提供语义值 export const apiKey = ‘SOMEKEY’;
// 多数情况下,很好 export const API_KEY = ‘SOMEKEY’;
// —-
// bad - 不必要大写 key 没有增加语义值 export const MAPPING = { KEY: ‘value’ };
// good export const MAPPING = { key: ‘value’ }; ```