1、文件夹命名规范

1.1 页面命名

使用小写开头的驼峰
2021-11-04_195659.png

1.2 组件命名

使用大写开头的驼峰
2021-11-04_195809.png

1.3 方法命名

使用小写开头的驼峰
2021-11-04_183357.png

2、CSS规范

2.1 BEM 命名规范

Bem 是块(block)、元素(element)、修饰符(modifier)的简写,由 Yandex 团队提出的一种前端 CSS 命名方法论。

2.2 BEM 命名约定的模式

  1. .block {}
  2. .block__element {}
  3. .block--modifier {}

2.3 示例

  1. <div class="article">
  2. <div class="article__body">
  3. <button class="article__button--primary"></button>
  4. <button class="article__button--success"></button>
  5. </div>
  6. </div>

2.4 目前项目中使用的规范

模块最外层使用文件夹相关的字段命名,保证模块的class是唯一的;
2021-11-04_181313.png

3、注释规范

3.1 函数名使用 / 注释文字 /, 函数内部使用 // 注释文字;

2021-11-04_181749.png

3.2 ts类型注释 使用 / 注释文字 /

2021-11-04_182229.png

4、JS规范

4.1 使用 === 和 !== 而不是 == 和 !=

  1. 严格判断会检查对象的类型,避免隐式的类型转换
  1. 0 == false // true
  2. 0 == '0' // true
  3. 0 === false // false
  4. 0 === '0' // false

4.2 条件简写

  1. // bad
  2. if (collection.length > 0) {
  3. // ...
  4. }
  5. // good
  6. if (collection.length) {
  7. // ...
  8. }
  1. // bad
  2. const hasValue = value !== NONE ? true : false;
  3. const hasProducts = products.length > 0 ? true : false;
  4. // good
  5. const hasValue = value !== NONE;
  6. const hasProducts = products.length > 0;
  1. // bad
  2. const hasValue = value ? true : false;
  3. const hasProducts = products.length ? true : false;
  4. // good
  5. const hasValue = Boolean(value);

4.3 布尔值转换

  1. const age = 0
  2. // bad
  3. const hasAge = new Boolean(age)
  4. // good
  5. const hasAge = Boolean(age)
  6. // good
  7. const hasAge = !!age

4.4 使用字符串模板

  1. const name = 'lucy'
  2. // bad
  3. const greetings = 'Hello ' + name
  4. // good
  5. const greetings = `Hello ${name}`

4.5 函数默认值

  1. // bad
  2. function handleThings(opts) {
  3. opts = opts || {}
  4. // ...
  5. }
  6. // good
  7. function handleThings(opts = {}) {
  8. // ...
  9. }

4.6 使用箭头函数

  1. // bad
  2. [1, 2, 3].map(function (x) {
  3. this.count += x
  4. return x * x
  5. }, this)
  6. // good
  7. [1, 2, 3].map(x => {
  8. this.count += x
  9. return x * x
  10. })

4.7 如果函数体只有一行返回语句,省略花括号和 return

  1. // bad
  2. [1, 2, 3].map((x) => {
  3. return x * x
  4. })
  5. // good
  6. [1, 2, 3].map(x => x * x)

4.8 多层if判断优化

  1. // bad
  2. if (month === A) {
  3. const m = dd;
  4. const s= hh;
  5. ......
  6. ......
  7. } else if(month === B){
  8. const j = dd;
  9. const k= hh;
  10. ......
  11. ......
  12. }
  13. // good
  14. const doSomethingA = (param) => {
  15. const m = dd;
  16. const s= hh;
  17. ......
  18. ......
  19. }
  20. const doSomethingB = (param) => {
  21. const j = dd;
  22. const k= hh;
  23. ......
  24. ......
  25. }
  26. if (month === A) {
  27. doSomethingA(param);
  28. } else if(month === B){
  29. doSomethingB(param);
  30. }

4.9 错误提前返回

  1. // bad
  2. const verifyForm = async (val) => {
  3. if(val.trim() !== '') {
  4. const id = val.id;
  5. ......
  6. } else {
  7. console.log('请输入id');
  8. }
  9. }
  10. // good
  11. const verifyForm = async (val) => {
  12. if(val.trim() !== '') {
  13. console.log('请输入id');
  14. return;
  15. }
  16. const id = val.id;
  17. ......
  18. }

4.10 禁止大函数

5、进阶(高标准)

5.1 单一数据源

  1. const persons = [{name: 'liu', age: 18}, {name: 'wang', age: 22}];
  2. // bad
  3. const newPersions = persons.map(k => {
  4. k.hobby = 'swimming';
  5. return k;
  6. });
  7. // good
  8. const newPersions = persons.map(k => ({
  9. ...k,
  10. hobby: 'swimming'
  11. }));
  1. // bad
  2. function addPersions(persions) {
  3. persions.push({
  4. name: '吴',
  5. age: 20
  6. });
  7. }
  8. addPersions(persons);
  9. // good
  10. function addPersions(persions) {
  11. return [...persions, {
  12. name: '吴',
  13. age: 20
  14. }]
  15. }
  16. addPersions(persons);
  1. const persons = [{name: 'liu', age: 18}, {name: 'wang', age: 22}];
  2. // bad
  3. function sortPersions(persions) {
  4. // sort有副作用,会改变原数组
  5. const sortArr = persions.sort((a, b) => b.age - a.age);
  6. return sortArr;
  7. }
  8. sortPersions(persons);
  9. // good
  10. function sortPersions(persions) {
  11. // sort有副作用,会改变原数组
  12. const sortArr = [...persions].sort((a, b) => b.age - a.age);
  13. return sortArr;
  14. }
  15. sortPersions(persons);
  1. // very bad
  2. const prev = { coffee: 1 };
  3. const next = Object.assign(prev, { pizza: 42 });
  4. // bad
  5. const prev = { coffee: 1 };
  6. const next = Object.assign({}, prev, { pizza: 42 });
  7. // good
  8. const prev = { coffee: 1 };
  9. const next = { ...prev, pizza: 42 };

5.2 单项数据流

  1. import React from 'react';
  2. class Parent extends React.Component {
  3. state = {
  4. list: [3, 4, 5],
  5. };
  6. handleAdd = (value) => {
  7. const list = this.state.list;
  8. this.setState({ list: [...list, value] });
  9. };
  10. render() {
  11. return (
  12. <div>
  13. <Child list={this.state.list} onAdd={this.handleAdd} />
  14. </div>
  15. );
  16. }
  17. }
  18. class Child extends React.Component {
  19. render() {
  20. const { list, onAdd } = this.props;
  21. return (
  22. <div>
  23. {list.map((item, index) => {
  24. return <div key={index}>{item.name}</div>;
  25. })}
  26. <button onClick={() => onAdd(6)} />
  27. </div>
  28. );
  29. }
  30. }

5.3 纯函数

  1. 1. 相同的输入一定会返回相同的输出
  2. 2. 永远不能修改传进去的值

7、作业

数组中有哪些方法会改变原始数据?