js代码上的优化
例1 if ( config.url === '/web/teacher/classes' || config.url === '/web/teacher/students || config.url === '/web/teacher/groups ) { .... }
// 优化
let urlOption = ['/web/teacher/classes', '/web/teacher/students', '/web/teacher/groups']
if (urlOption.includes(config.url)) { ...}
// 类似
let str = ''
switch (number) {
case 0:str = '编辑'
break
case 1:str = '新增'
break
}
return str
let desArr = ['编辑', '新增']
return desArr[nubmer]
例2 var str = '', type = 'sex'
switch (type) {
case 'name':str = '姓名'
break
case 'sex':str = '性别'
break
}
function fn(val){
if(!val) return ''
let dict = {
name: '小明',
sex: '女'
}
return dict[val] || val
}
fn('name')
// 用对象字面量会更好,也更方便维护和复用。比较常见的是一些枚举数据以及固定值。