逻辑判断之入门——If else
const run = (key: number) => {
if(key === 1){
console.log('1')
}else if(key === 2){
console.log('2')
}else{
console.log('3')
}
}
逻辑判断之入门——Switch
const run = (key: number) => {
switch(key){
case 1:
console.log('1');
break;
case 2:
console.log('2');
break;
case 3:
console.log('3');
break;
default:
break;
}
}
逻辑判断之进阶——单一判断、Switch与Map
const map = new Map([
[1, ()=> {console.log('1')}],
[2, ()=> {console.log('2')}],
[3, ()=> {console.log('3')}]
])
const run = (key: number) => {
const func = map.get(key);
if(!func) return;
func();
}
逻辑判断之高级——多层判断、Switch与Map
背景:上述的🌰 都是基于单层判断的,下面给出一个多层判断的🌰 :
const run = (key: number, status: string) => {
if(key === 1){
switch(status){
case 'success':
console.log('key: 1, status: success');
break;
case 'fail':
console.log('key: 1, status: fail');
break;
}
}else if(key === 2){
switch(status){
case 'success':
console.log('key: 2, status: success');
break;
case 'fail':
console.log('key: 2, status: fail');
break;
}
}
}
下面进行优化:
使用Map优化
const func = (key: number, status: string) => {
console.log(`key: ${key}, status: ${status}`);
}
const map = new Map([
[{key: 1, status: 'success']: ()=> func(1, 'success'),
[{key: 2, status: 'success']: ()=> func(2, 'success'),
[{key: 1, status: 'fail']: ()=> func(1, 'fail'),
[{key: 2, status: 'fail']: ()=> func(2, 'fail')
])
const run = (key: number, status: string) => {
let cur = Array.from(map).find(item=>item[0].key === number && item[0].status===status);
if(cur && cur[1]){
cur[1].call(this);
}
}
缺点:如果有10种key,20种status,那么在map里需要写200个逻辑。
使用正则表达式
const func = (key: number, status: string) => {
console.log(`key: ${key}, status: ${status}`);
}
const map = new Map([
[/^success_[1-4]$/]: ()=> func(key, 'success'),
[/^success_.*$/]: ()=> func(key, 'success'),
])
const run = (key: number, status: string) => {
let cur = Array.from(map).find(item=>item[0].test(`${status}_${number}`));
if(cur && cur[1]){
cur[1].call(this);
}
}
这里使用了正则表达式来作为key,这也是为什么要用map而不是object的原因。
逻辑判断之高级——多层判断、策略模式
策略模式的定义:定义一系列的算法,独立进行封装,并使他们可以相互替换。
下面举个例子:
const run = (key: number, status: string) => {
if(key === 1){
switch(status){
case 'success':
console.log('key: 1, status: success');
break;
case 'fail':
console.log('key: 1, status: fail');
break;
}
}else if(key === 2){
switch(status){
case 'success':
console.log('key: 2, status: success');
break;
case 'fail':
console.log('key: 2, status: fail');
break;
}
}
}
// 下面使用组合函数进行一些优化:将各种算法进行了封装,但后续更改不太灵活
const getSuccessStatusText = (key: number) => {
return `key: ${key}, status: success`
}
const getFailStatusText = (key: number) => {
return `key: ${key}, status: fail`
}
const run = (key: number, status: string) => {
if(status === 'success'){
console.log(getSuccessStatusText(key))
}else if(status === 'fail'){
console.log(getSuccessStatusText(key))
}
}
run(1, 'success')
// 下面使用策略模式
const statusAction = {
"success": function(key: number)=>{
return `key: ${key}, status: success`
},
"fail": function(key: number)=>{
return `key: ${key}, status: fail`
}
}
const run = (key: number, status: string) => {
console.log(statusAction[status](key))
}
run(1, 'success')
总结
- 单层判断时,使用Switch,可以根据自己喜好加入Map
- 多层判断时,使用Switch+Map+正则表达式,策略模式