文件组织形式

  1. ├── .husky husky
  2. ├── .vscode vscode配置
  3. ├── config 配置目录
  4. ├── patches 补丁目录
  5. ├── bin 自定义脚本目录
  6. ├── public html和静态资源目录
  7. ├── typings 全局类型或第三方库类型文件
  8. ├── src 源码目录
  9. | ├── components 公共组件目录
  10. | ├── assets 资源目录
  11. | ├── hooks 公共hooks
  12. | ├── utils 公共方法库
  13. | ├── pages 页面文件目录
  14. | | ├── index index 页面目录
  15. | | | ├── components 页面 index 私有组件目录
  16. | | | ├── hooks 页面 index 私有自定义hooks
  17. | | | ├── index.tsx index 页面逻辑
  18. | | | └── index.less index 页面样式
  19. | | | └── api.ts 接口
  20. | | | └── store.ts redux store文件
  21. | | | └── types.ts 类型定义
  22. | | | └── slice.ts redux slice文件
  23. | | | └── const.ts 常量定义文件
  24. | | | └── util.ts 方法文件
  25. | ├── app.less 项目通用样式
  26. | └── app.tsx 项目入口文件
  27. └── .editorconfig
  28. └── .eslintrc.js
  29. └── .gitignore
  30. └── .prettierrc.js
  31. └── .stylelintrc.js
  32. └── tsconfig.json
  33. └── README.md
  34. └── package.json

命名/后缀

普通文件以小写字母命名,多个单词以下划线连接,例如 util.js、util_helper.js

普通文件以 .ts 作为文件后缀,React模块文件以.tsx作为后缀。

React模块组件文件命名遵循大驼峰命名法,例如 ReservationCard.jsx

引用命名: React模块名使用大驼峰命名,实例使用驼峰命名

  1. // bad
  2. import reservationCard from './ReservationCard';
  3. // good
  4. import ReservationCard from './ReservationCard';
  5. // bad
  6. const ReservationItem = <ReservationCard />;
  7. // good
  8. const reservationItem = <ReservationCard />;

模块命名:

模块使用当前文件名一样的名称. 比如 ReservationCard.jsx 应该包含名为 ReservationCard的模块. 但是,如果整个文件夹是一个模块,使用 index.js作为入口文件,然后直接使用 index.js 或者文件夹名作为模块的名称:

  1. // bad
  2. import Footer from './Footer/Footer';
  3. // bad
  4. import Footer from './Footer/index';
  5. // good
  6. import Footer from './Footer';

属性命名:

避免使用DOM相关的属性来用作其他的用途。

为什么?对于styleclassName这样的属性名,我们都会默认它们代表一些特殊的含义,如元素的样式,CSS class的名称。在你的应用中使用这些属性来表示其他的含义会使你的代码更难阅读,更难维护,并且可能会引起bug。

  1. // bad
  2. <MyComponent style="fancy" />
  3. // good
  4. <MyComponent variant="fancy" />

避免用一个字母命名,让你的命名有意义。

  1. // bad
  2. function q() {
  3. // ...
  4. }
  5. // good
  6. function query() {
  7. // ...
  8. }

用驼峰命名法来命名你的对象、函数、实例。

  1. // bad
  2. const OBJEcttsssss = {};
  3. const this_is_my_object = {};
  4. function c() {}
  5. // good
  6. const thisIsMyObject = {};
  7. function thisIsMyFunction() {}

用大驼峰命名法来命名类。

  1. // bad
  2. function user(options) {
  3. this.name = options.name;
  4. }
  5. const bad = new user({
  6. name: 'nope',
  7. });
  8. // good
  9. class User {
  10. constructor(options) {
  11. this.name = options.name;
  12. }
  13. }
  14. const good = new User({
  15. name: 'yup',
  16. });

不要用前置或后置下划线。

为什么?JavaScript 没有私有属性或私有方法的概念。尽管前置下划线通常的概念上意味着私有,事实上,这些属性是完全公有的,因此这部分也是你的 API 的内容。这一概念可能会导致开发者误以为更改这个不会导致崩溃或者不需要测试。如果你想要什么东西变成私有,那就不要让它在这里出现。

  1. // bad
  2. this.__firstName__ = 'Panda';
  3. this.firstName_ = 'Panda';
  4. this._firstName = 'Panda';
  5. // good
  6. this.firstName = 'Panda';

导出名和文件名一致

export default 导出模块A,则这个文件名也叫 A.*import 时候的参数也叫 A。 大小写完全一致。

  1. // file 1 contents
  2. class CheckBox {
  3. // ...
  4. }
  5. export default CheckBox;
  6. // file 2 contents
  7. export default function fortyTwo() { return 42; }
  8. // file 3 contents
  9. export default function insideDirectory() {}
  10. // in some other file
  11. // bad
  12. import CheckBox from './checkBox'; // PascalCase import/export, camelCase filename
  13. import FortyTwo from './FortyTwo'; // PascalCase import/filename, camelCase export
  14. import InsideDirectory from './InsideDirectory'; // PascalCase import/filename, camelCase export
  15. // bad
  16. import CheckBox from './check_box'; // PascalCase import/export, snake_case filename
  17. import forty_two from './forty_two'; // snake_case import/filename, camelCase export
  18. import inside_directory from './inside_directory'; // snake_case import, camelCase export
  19. import index from './inside_directory/index'; // requiring the index file explicitly
  20. import insideDirectory from './insideDirectory/index'; // requiring the index file explicitly
  21. // good
  22. import CheckBox from './CheckBox'; // PascalCase export/import/filename
  23. import fortyTwo from './fortyTwo'; // camelCase export/import/filename
  24. import insideDirectory from './insideDirectory'; // camelCase export/import/directory name/implicit "index"
  25. // ^ supports both insideDirectory.js and insideDirectory/index.js

函数名用小驼峰,文件名需要和函数名一致。

  1. function makeStyleGuide() {
  2. // ...
  3. }
  4. export default makeStyleGuide;

类/单例/函数库/对象 用大驼峰。

  1. const AirbnbStyleGuide = {
  2. es6: {
  3. }
  4. };
  5. export default AirbnbStyleGuide;

全大写字母设置静态变量,

  • 他需要满足三个条件。
    1. 导出变量;
    2. const 定义的, 保证不能被改变;
    3. 这个变量是可信的,他的子属性都是不能被改变的。

      为什么?这是一个附加工具,帮助开发者去辨识一个变量是不是不可变的。UPPERCASE_VARIABLES 能让开发者知道他能确信这个变量(以及他的属性)是不会变的。

  1. // bad
  2. const PRIVATE_VARIABLE = 'should not be unnecessarily uppercased within a file';
  3. // bad
  4. export const THING_TO_BE_CHANGED = 'should obviously not be uppercased';
  5. // bad
  6. export let REASSIGNABLE_VARIABLE = 'do not use let with uppercase variables';
  7. // ---
  8. // 允许但不够语义化
  9. export const apiKey = 'SOMEKEY';
  10. // 在大多数情况下更好
  11. export const API_KEY = 'SOMEKEY';
  12. // ---
  13. // bad - 不必要的大写键,没有增加任何语义
  14. export const MAPPING = {
  15. KEY: 'value'
  16. };
  17. // good
  18. export const MAPPING = {
  19. key: 'value'
  20. };

[

](#%E7%9B%AE%E5%BD%95)