一、定义
表驱动方法(Table-Driven Approach)是一种使你可以在表中查找信息,而不必用很多的逻辑语句(if或case)来把它们找出来的方法。事实上,任何信息都可以通过表来挑选。在简单的情况下,逻辑语句往往更简单而且更直接。
简单讲是指用查表的方法获取值。 我们平时查字典以及念初中时查《数学用表》找立方根就是典型的表驱动法。在数值不多的时候我们可以用逻辑语句(if 或case)的方法来获取值,但随着数值的增多逻辑语句就会越来越长,此时表驱动法的优势就显现出来了。(百度百科)
二、数组中的应用
假设你需要一个可以返回每个月中天数的函数(为简单起见不考虑闰年)
使用if else
function iGetMonthDays(iMonth) {
let iDays;
if(1 == iMonth) {iDays = 31;}
else if(2 == iMonth) {iDays = 28;}
else if(3 == iMonth) {iDays = 31;}
else if(4 == iMonth) {iDays = 30;}
else if(5 == iMonth) {iDays = 31;}
else if(6 == iMonth) {iDays = 30;}
else if(7 == iMonth) {iDays = 31;}
else if(8 == iMonth) {iDays = 31;}
else if(9 == iMonth) {iDays = 30;}
else if(10 == iMonth) {iDays = 31;}
else if(11 == iMonth) {iDays = 30;}
else if(12 == iMonth) {iDays = 31;}
return iDays;
}
使用表驱动(包括闰年判断)
const monthDays = [
[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
[31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
]
function getMonthDays(month, year) {
let isLeapYear = (year % 4 === 0) && (year % 100 !== 0 || year % 400 === 0) ? 1 : 0
return monthDays[isLeapYear][(month - 1)];
}
console.log(getMonthDays(2, 2000))
三、对象中的应用
形如:
if (key = "Key A")
{
处理 Key A 相关的数据。
}
else if (key = "Key B")
{
处理 Key B 相关的数据。
}
或
if (key = "Key A")
{
执行 Key A 相关的行为。
}
else if (key = "Key B")
{
执行 Key B 相关的行为。
}
可以改装成:
let table = {
A: {
data: "数据1",
action: "行为1"
},
B: {
data: "数据2",
action: "行为2"
}
}
function handleTable(key) {
return table[key]
}
console.log(handleTable('A').data)
或
let table = {
A: {
data: "数据1",
action () {
console.log('action 1')
}
},
B: {
data: "数据2",
action () {
console.log('action 2')
}
}
}
function handleTable(key) {
return table[key]
}
handleTable('A').action()
回顾一下我以前写的代码,好多复杂的代码都通过这种方式简化了不少,比如:
let vm = this
let adTable = {
bdkq () {
window.clLib.localStorageOperating.updateData('default_hospital_code', '871978')
vm.$router.push('/hospital')
},
etyy () { vm.goSp(10012) },
szn () { vm.goCh('szn') }
}
adTable[data.key] ? adTable[data.key]() : location.href = data.ad_url
以前是怎么写的呢? 看看就知道了,特别复杂!
switch (data.key) {
case 'bdkq':
window.clLib.localStorageOperating.updateData('default_hospital_code', '871978')
this.$router.push('/hospital')
break
case 'etyy':
this.goSp(10012)
break
case 'szn':
this.goCh('szn')
break
default:
location.href = data.ad_url
break
}
四、总结
好的代码总是将复杂的逻辑以分层的方式降低单个层次上的复杂度。复杂与简单有一个相互转化的过程。(CSDN)