我有这种多嵌套的 if-else 块。我的理解是,有一种“数据驱动”的方法可以帮助消除对它的需求并精简代码,但是,我还没有大量使用它的经验,所以任何人都可以帮我重构这段代码以“数据驱动”的方式工作?
function (arg1) {
if(this.thing[arg1]){
// there is a valid property for this arg1
if(this.thing.a){
// there exists an 'a' propertie also
if(this.thing.a.arg1 == arg1){
// the a property has a property is the same as the arg1
// if this 'a' has a number higher than 0, avoid doing anything
if(this.thing.a.number > 0){
}else{
// 'a' number was 0 or lower, so we do something
this.thing.a = this.thing[arg1];
this.thing.a.arg1 = arg1;
}
}else{
// the' a' is not the arg1
// so we want to use the current arg1!
// but only if its 'number' is lower than 1
if(this.thing.a.number > 0){
}else{
// 'a' number was 0 or lower, so we do something
this.thing.a = this.thing[arg1];
this.thing.a.arg1 = arg1;
}
}
}else{
// there is no thing.a so we set it to arg1
this.thing.a = this.thing[arg1];
this.thing.a.arg1 = arg1;
}
}
}