-
可读性、可维护性、提升性能
// -----------
// 1、合理使用 对象
// -----------
// 多种条件(初始)
const a = 1
let res
switch (a) {
case 1:
res = 'one'
break
case 2:
res = 'two'
break
// 超级多...
}
console.log(res) // one
//利用 对象(优化)
const a = 1
const map = {
'1': 'one',
'2': 'two',
// ...
}
let res = map[a]
// -----------
// 2、合理使用 数组 + includes
// -----------
// 多个或进行if判断(初始)
const a = 1
if (a === 1 || a === 2 || a === 3) {
// ...
}
// 利用 数组+includes(优化)
const a = 1
if ([1, 2, 3].includes(a)) {
// ...
}
// -----------
// 3、合理使用
// -----------
// -----------
// 4、合理使用
// -----------
// -----------
// 5、合理使用
// -----------
// -----------
// 6、合理使用
// -----------