参考

1
2
3
4
代码规范:
1. 基础规范:http://alloyteam.github.io/CodeGuide/
2. Vue 规范:https://v2.cn.vuejs.org/v2/style-guide/
3. ES6 规范:https://es6.ruanyifeng.com

代码质量检查工具

  • 项目自带的 vue-cli-service lint
  • 项目安装的第三方 lint 工具,配置脚本执行,如 《ESLint》中 F2ELint
  • 使用 IDEA 自带工具 Analyze Inspect Code,最全面的代码质量检查工具,包含自带和第三方 lint 工具

Airbnb

https://github.com/airbnb/javascript

块级作用域

  1. 建议不再使用 var ,而是使用 let 取代

  2. letconst 之间,建议优先使用 const,利于提高程序的运行效率

  3. 所有的函数都应该设置为常量 const,避免重复定义

  4. 常量应全部大写,通过下划线 _ 连接,例如 const MAX_USER_COUNT = 200

  5. const 对象不能重新赋值,但可以操作对象

    1
    2
    3
    4
    5
    6
    const p = {name: "张三"}
    p.name = "李四"
    p.age = 20;

    //更好
    Object.assign(p, { age: 30 })

字符串

静态字符串一律使用单引号或反引号,不使用双引号。动态字符串使用反引号,${variable} 引用变量值

1
2
3
const a = 'foobar';

const b = `foo${a}bar`;

解构赋值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//对数组成员变量赋值时,优先使用解构赋值
const arr = [1, 2, 3, 4];
const [first, second] = arr;

//对对象的成员
const { left, right } = {left: "left", right: "right"}

const { left: leftV, right: rightV } = {left: "left", right: "right"}
// leftV left
// rightV right

//函数的参数如果是对象的成员,优先使用解构赋值
function getFullName({ firstName, lastName }) {

}
getFullName({firstName: 'zhang', lastName:'ming'})

//解构复杂对象
const json = { name:'张三', age:20, result: { code: 200} };
const { name, age, result: { code }} = json;
console.log(code); //200
console.log(name) //张三

对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//单行定义的对象,最后一个成员不以逗号结尾。多行定义的对象,最后一个成员以逗号结尾
const a = { k1: v1, k2: v2 };
const b = {
k1: v1,
k2: v2,
};

//复制
const p = {name: "张三"}
const k = {...p}

const p = [1,2,3]
const k = [...p]

//对象尽量静态化,一旦定义,就不得随意添加新的属性。如果添加属性不可避免,要使用 Object.assign 方法
const a = { x: null };
a.x = 3;

const a = {name: '张三'};
Object.assign(a, { x: 3 });
let b = {...a, {x: 3}}

//对象的属性和方法,尽量采用简洁表达法
const atom = {
ref,
value: 1,
addValue(value) {
return atom.value + value;
},
};

数组

1
2
3
4
5
6
7
8
//使用扩展运算符(...)拷贝数组
const itemsCopy = [...items];
const items2 = [...items, '1', '2'];
const items3 = ['1', '2', ...items];

//使用 Array.from 方法,将类似数组的对象转为数组
const foo = document.querySelectorAll('.foo');
const nodes = Array.from(foo);

函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//匿名函数使用箭头函数: 简洁,而且绑定了 this,不再用 self/_this/that 绑定 this
[1, 2, 3].map(x => x * x);


//函数参数接收多个参数,使用 ... 运算符
function concatenateAll(name, age, ...args) {
return args.join('');
}

//默认值语法设置函数参数的默认值: 带默认的参数后面不能有无默认值参数,也就是带默认值参数是排在后面的
function handleThings(name, age=20) {
// ...
}
function handleThings(name, age=20, sex="男") {
// ...
}
function handleThings(opts = {}) {
// ...
}

Map

只有模拟现实世界的实体对象时,才使用 Object。如果只是需要 key: value 的数据结构,使用 Map 结构。因为 Map 有内建的遍历机制

只有模拟现实世界的实体对象时,才使用 Object。如果只是需要 key: value 的数据结构,使用 Map 结构。因为 Map 有内建的遍历机制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const map = new Map();
map.set('a', 1);
map.set('b', 2);
map.set('c', 3);

console.log(map.get('a'));
// expected output: 1

map.set('a', 97);
console.log(map.get('a'));
// expected output: 97

console.log(map.size);
// expected output: 3

map.delete('b');
console.log(map.size);
// expected output: 2

for (let key of map.keys()) {
console.log(key); // a b c
}

for (let value of map.values()) {
console.log(value); // 1 2 3
}

for (let item of map.entries()) {
console.log(item[0], item[1]);
}
//a 1
//b 2
//c 3

Class

用 Class,取代需要 prototype 的操作,Class 的写法更简洁

1
2
3
4
5
6
7
8
9
10
11
// good
class Queue {
constructor(contents = []) {
this._queue = [...contents];
}
pop() {
const value = this._queue[0];
this._queue.splice(0, 1);
return value;
}
}

Extend

使用 extends 实现继承,因为这样更简单,不会有破坏 instanceof 运算的危险

1
2
3
4
5
6
// good
class PeekableQueue extends Queue {
peek() {
return this._queue[0];
}
}

模块

Module 语法是 JavaScript 模块的标准写法,坚持使用这种写法。使用 import 取代 require

1
import { func1, func2 } from 'moduleA';

使用 export 取代 module.exports

1
2
3
4
5
6
7
8
9
10
// ES6的写法
import React from 'react';

class Breadcrumbs extends React.Component {
render() {
return <nav />;
}
};

export default Breadcrumbs;

ESLint 的使用

ESLint 是一个语法规则和代码风格的检查工具,可以用来保证写出语法正确、风格统一的代码