优化多if和else语句
2024-09-08 19:25:13
可以利用对象来进行映射
比如
let type = this.radio_value
if (type === 'whole') {
this.time_value = []
} else if (type === 'today') {
this.time_value = [this.getDay(0, '-'), this.getDay(1, '-')]
} else if (type === 'yesterday') {
this.time_value = [this.getDay(-1, '-'), this.getDay(0, '-')]
} else if (type === 'last_seven_days') {
this.time_value = [this.getDay(-7, '-'), this.getDay(0, '-')]
} else if (type === 'last_thirty_days') {
this.time_value = [this.getDay(-30, '-'), this.getDay(0, '-')]
}
可以优化成
let type = this.radio_value
let map = {
'whole':()=>this.time_value = [],
'today':()=>this.time_value = [this.getDay(0, '-'), this.getDay(1, '-')],
'yesterday':()=>this.time_value = [this.getDay(-1, '-'), this.getDay(0, '-')],
'last_seven_days':()=>this.time_value = [this.getDay(-7, '-'), this.getDay(0, '-')],
'last_thirty_days':()=>this.time_value = [this.getDay(-30, '-'), this.getDay(0, '-')],
}
if(map[type]){
map[type]()
}
最新文章
- Windows消息机制
- HTML5全屏(Fullscreen)API详细介绍
- jquery1.7.2的源码分析(二)
- Unity 3D 基础知识方法
- 【.Net底层剖析】3.用IL来理解属性
- (转) 一步一步学习ASP.NET 5 (一)- 基本概念和环境配置
- 重温WCF之消息拦截与篡改(八)
- java快速排序详解
- homebrew 无法安装提示不能在根目录下使用
- wpf研究之道——自定义Button控件
- 为什么要使用TypeScript开发Web应用程序
- docker网络
- Java 多线程之悲观锁与乐观锁
- call,apply,bind的用法与区别
- Python设计模式 - UML - 用例图(Use Case Diagram)
- 【netcore基础】.Net core使用swagger自动生成开发文档
- JavaScript中var和this定义变量的区别
- 20145118 《Java程序设计》 实验报告四
- Office - Excel 2013
- CentOS安装oracleJDK
热门文章
- java介绍、环境搭建与Hello,World!
- 1、Spring源码环境搭建
- DataX二次开发——新增HiveReader插件
- .NET 团队 更新了 .NET 语言策略
- Windows离线安装.net Framework3.5
- 数字游戏(NOIP 2003 PJT2)
- Windows Server 2016 安装AD和Exchange
- 使用GetDIBits()获取Windows位图数据的标准用法,解决内存、堆栈报错问题
- SpringMVC的文件、数据校验(Vaildator、Annotation JSR-303)
- 大曝光!从RabbitMQ平滑迁移至Kafka架构设计方案!