分支语句优化--状态模式
发表日期:2019-01-25
// 单状态条件判断 每增加一个状态就需要添加一个判断
function Foo1 (status) {
if(status === 1) {
// do sth1
} else if(status === 2) {
// do sth2
}else {
// do sth3
}
}
// 复合状态对条件判断的开销是翻倍的
function Foo2 (status1, status2) {
if (status1 === 1) {
// do sth1
} else if (status1 === 2) {
// do sth2
} else if (status1 === 1 && status2 === 2) {
// do sth1 and sth2
} else if (status1 === 2 && status2 == 3) {
// do sth2 and sth3
}
}模式实例
取巧Map
小结
最后更新于