重复if / else部分简化功能

时间:2015-10-14 22:35:54

标签: javascript

我正在尝试简化此功能,因为可以有多个type数据对象,并且对于每种类型,还有男性和女性版本。 对象中元素的数量和名称始终相同。

如您所见,大部分代码都在重复......

function calculate(type, j, value, s) {
    for (var i = j; i > 4; i--) { 
        if (type == 'weight') {
            if (s == 'f') { 
                if (weightFemale.hasOwnProperty(i)) {
                    var m = weightFemale[i][0],
                        l = weightFemale[i][1],
                        s = weightFemale[i][2];     
                    return getcalc( m,l,s );
                }
            }
            else {
                if (weightMale.hasOwnProperty(i)) {
                    var m = weightMale[i][0],
                        l = weightMale[i][1],
                        s = weightMale[i][2];       
                    return getcalc( m,l,s );
                }               
            }
        }
        else if (type == 'length') {
            if (s == 'f') { 
                if (lengthFemale.hasOwnProperty(i)) {
                    var m = lengthFemale[i][0],
                        l = lengthFemale[i][1],
                        s = lengthFemale[i][2],
                    return getcalc( m,l,s );
                }
            }
            else {
                if (lengthMale.hasOwnProperty(i)) {
                    var m = lengthMale[i][0],
                        l = lengthMale[i][1],
                        s = lengthMale[i][2],
                    return getcalc( m,l,s );
                }               
            }
        }
    }
    return false;
}

如何简化类型和性别的if / else部分?

4 个答案:

答案 0 :(得分:1)

我会创建一个自己的函数并创建一个开关

switch(type) {
    case "weight":
        getValues();
        break;
    case "length":
        getValues();
        break;
}

答案 1 :(得分:1)

由于您对每个对象执行相同的操作,只需使条件定义单个对象引用并仅调用一次计算。

类似的东西:

var obj;

if (type == 'weight') {
    obj = s == 'f' ? weightFemale : weightMale;
} else if (type == 'length') {
    obj = s == 'f' ? lengthFemale : lengthMale;
}

if (obj.hasOwnProperty(i)) {
    var m = obj[i][0],
        l = obj[i][1],
        s = obj[i][2];
    return getcalc(m, l, s);
}

答案 2 :(得分:0)

问问自己,哪些部件非常相似?看看.hasOwnProperty()是否为真,你从数组中得到m,l和s,然后调用getcalc()。首先将该部分提取到一个函数中,将不同的部分作为参数传入。

另一种模式是您根据特定条件使用特定阵列。获取所需的数组可以放入函数中。

此外,与问题并不完全相关,但您可能希望为变量提供更好的名称。这使代码更易读,更容易推理。

这是我想出的:

function getArray(type, s){
    if(type == 'weight') {
        return s == 'f' ? weightFemale : weightMale;
    }
    else if(type == 'length') {
        return s == 'm' ? lengthFemale : lengthMale;    
    }
}


function makeCalculation(array, i) {
    if(array.hasOwnProperty(i)) {
        var m = lengthMale[i][0],
            l = lengthMale[i][1],
            s = lengthMale[i][2],
        return getcalc( m,l,s );
    }
}

function calculate(type, j, value, s) {
    for (var i = j; i > 4; i--) { 
        var array = getArray(type, s);
        return makeCalculation(array, i);
    }
}

答案 3 :(得分:0)

首先,你可以创建这样的函数:

Format(e.Row.Cells(7).Text, "{0:n}")

然后你可以缩短你的功能:

Format(e.Row.Cells(7).Text, "Currency")