代码规范

代码规范主要作用是规范开发、提高开发效率、提高代码可读性以及便于后期维护。

链接 链接

引用

使用const、let 代替var

let const 是块级作用域,而不像 var 是函数作用域.

  1. // const 和 let 只存在于他们定义的块中。
  2. {
  3. let a = 1;
  4. const b = 1;
  5. }
  6. console.log(a); // Error
  7. console.log(b); // Error

  1. //内层变量覆盖外层变量
  2. var tmp = new Date();
  3. function f(){
  4. console.log(tmp);
  5. if(false){
  6. var tmp = "hello";
  7. }
  8. }
  9. f(); //undefined

image.png

注释与空白

1、使用 /** ... */ 来进行多行注释。

  1. // bad
  2. // make() returns a new element
  3. // based on the passed in tag name
  4. //
  5. // @param {String} tag
  6. // @return {Element} element
  7. function make(tag) {
  8. // ...
  9. return element;
  10. }
  11. // good
  12. /**
  13. * make() returns a new element
  14. * based on the passed-in tag name
  15. */
  16. function make(tag) {
  17. // ...
  18. return element;
  19. }

2、使用 // 进行单行注释。 将单行注释放在需要注释的行的上方新行。 在注释之前放一个空行,除非它在块的第一行。

  1. // bad
  2. const active = true; // is current tab
  3. function getType() {
  4. console.log('fetching type...');
  5. // set the default type to 'no type'
  6. const type = this.type || 'no type';
  7. return type;
  8. }
  9. // good
  10. // is current tab
  11. const active = true;
  12. function getType() {
  13. console.log('fetching type...');
  14. // set the default type to 'no type'
  15. const type = this.type || 'no type';
  16. return type;
  17. }
  18. function getType() {
  19. // set the default type to 'no type'
  20. const type = this.type || 'no type';
  21. return type;
  22. }

3、 用一个空格开始所有的注释。

  1. // bad
  2. /**
  3. *make() returns a new element
  4. *based on the passed-in tag name
  5. */
  6. function make(tag) {
  7. // ...
  8. return element;
  9. }
  10. // good
  11. /**
  12. * make() returns a new element
  13. * based on the passed-in tag name
  14. */
  15. function make(tag) {
  16. // ...
  17. return element;
  18. }

4、在主体前放置一个空格。

  1. // bad
  2. function test(){
  3. console.log('test');
  4. }
  5. // good
  6. function test() {
  7. console.log('test');
  8. }
  9. // bad
  10. dog.set('attr',{
  11. age: '1 year',
  12. breed: 'Bernese Mountain Dog',
  13. });
  14. // good
  15. dog.set('attr', {
  16. age: '1 year',
  17. breed: 'Bernese Mountain Dog',
  18. });

表达式、运算符

1、三目表达式不应该嵌套。

通常为单行表达式

  1. // bad
  2. const foo = maybe1 > maybe2
  3. ? "bar"
  4. : value1 > value2 ? "baz" : null;
  5. // 分离为两个三目表达式
  6. const maybeNull = value1 > value2 ? 'baz' : null;
  7. // better
  8. const foo = maybe1 > maybe2
  9. ? 'bar'
  10. : maybeNull;
  11. // best
  12. const foo = maybe1 > maybe2 ? 'bar' : maybeNull;

2、在使用一些混合运算符时,使用括号括起来。

  1. // bad
  2. const foo = a && b < 0 || c > 0 || d + 1 === 0;
  3. // bad
  4. // 可能陷入一种 (a || b) && c 的思考
  5. if (a || b && c) {
  6. return d;
  7. }
  8. // good
  9. const foo = (a && b < 0) || c > 0 || (d + 1 === 0);
  10. // good
  11. if (a || (b && c)) {
  12. return d;
  13. }

3、使用 ===!== 而不是 ==!=

image.png

命名规范

1、避免单字母的名字。用你的命名来描述功能。

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

2、在命名对象、函数和实例时使用驼峰命名法。

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

3、属性命名时不要使用前置或者后置下划线。

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

4、缩略词和缩写都必须是全部大写或者全部小写。

  1. // bad
  2. import SmsContainer from './containers/SmsContainer';
  3. // bad
  4. const HttpRequests = [
  5. // ...
  6. ];
  7. // good
  8. import SMSContainer from './containers/SMSContainer';
  9. // good
  10. const HTTPRequests = [
  11. // ...
  12. ];
  13. // also good
  14. const httpRequests = [
  15. // ...
  16. ];

模块

1、只从一个路径导入所有需要的东西。

  1. // bad
  2. import foo from 'foo';
  3. // … 其他导入 … //
  4. import { named1, named2 } from 'foo';
  5. // good
  6. import foo, { named1, named2 } from 'foo';
  7. // good
  8. import foo, {
  9. named1,
  10. named2,
  11. } from 'foo';

2、将所有的 imports 语句放在所有非导入语句的之上。

  1. // bad
  2. import foo from 'foo';
  3. foo.init();
  4. import bar from 'bar';
  5. // good
  6. import foo from 'foo';
  7. import bar from 'bar';
  8. foo.init();

3、多行导入应该像多行数组和对象一样缩进。

  1. // bad
  2. import {longNameA, longNameB, longNameC, longNameD, longNameE} from 'path';
  3. // good
  4. import {
  5. longNameA,
  6. longNameB,
  7. longNameC,
  8. longNameD,
  9. longNameE,
  10. } from 'path';

4、使用easycom 组件引入模式。

不管components目录下安装了多少组件,easycom打包后会自动剔除没有使用的组件,对组件库的使用尤为友好。

image.png
image.png

代码块

1、当有多行代码块的时候,使用大括号包裹。

  1. // bad
  2. if (test)
  3. return false;
  4. // good
  5. if (test) return false;
  6. // good
  7. if (test) {
  8. return false;
  9. }
  10. // bad
  11. function foo() { return false; }
  12. // good
  13. function bar() {
  14. return false;
  15. }

2、如果使用if、else的多行代码,else语句放在if闭括号同一行。

  1. // bad
  2. if (test) {
  3. thing1();
  4. thing2();
  5. }
  6. else {
  7. thing3();
  8. }
  9. // good
  10. if (test) {
  11. thing1();
  12. thing2();
  13. } else {
  14. thing3();
  15. }

3、if条件与return。

如果一个if块总是执行一个return 语句,接下来的else块便可以省略。

  1. // bad
  2. function foo() {
  3. if (x) {
  4. return x;
  5. } else {
  6. return y;
  7. }
  8. }
  9. // good
  10. function foo() {
  11. if (x) {
  12. return x;
  13. }
  14. return y;
  15. }

release模式下删除调试信息

链接https://cn.vuejs.org/v2/style-guide/#%E7%A7%81%E6%9C%89-property-%E5%90%8D%E5%BF%85%E8%A6%81