Javascript(Code Reivew) - 01

1. 空对象在数组中不认为是空

1
2
3
4
5
6
// output : this is not empty
if ({}) {
console.log("this is not empty");
} else {
console.log("this is empty");
}

2. prefer template 模式

链接地址 : https://eslint.org/docs/rules/prefer-template

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// before
const urlPart = Object.keys(params)
.map(function (k) {
return encodeURIComponent(k) + "=" + encodeURIComponent(params[k]);
})
.join("&");
// after
const urlPart = Object.keys(params)
.map((k) => {
const encodeA = encodeURIComponent(k);
const encodeB = encodeURIComponent(params[k]);
return `${encodeA}=${encodeB}`;
})
.join("&");

3. 不写行内函数 (no-inner-declarations)

链接地址: https://eslint.org/docs/rules/no-inner-declarations

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
34
35
36
37
38
39
40
41
42
43
44
45
// bad
const handleRender = (h, params) => {
if (params.row.can_handle === true) {
function btn(prop) {
return h('btn', {
props : prop,
on : {
click : () => {
that.onDoClick(params);
}
}
});
}
let prop = {};
prop = {
type : 'primary',
icon : 'ios-compose',
title : '待提现',
};
return btn(prop);
}
}
return '';
}
// good
const handleRender = (h, params) => {
if (params.row.can_handle === true) {
let prop = {};
prop = {
type : 'primary',
icon : 'ios-compose',
title : '待提现',
};
return h('btn', {
props : prop,
on : {
click : () => {
that.onDoClick(params);
}
}
});
}
}
return '';
}

4. 多余的 “,”

1
2
3
4
5
// bad
const { status, message, data } = response.data;

// good
const { status, message, data } = response.data;

语雀镜像 : Javascript(Code Reivew) - 01 ,点此 提问

作者

Duoli

发布于

2022-04-20

更新于

2022-09-30

许可协议

评论