将其放入函数JS

时间:2018-10-04 09:59:48

标签: javascript function conditional-statements

有一个条件,但是很麻烦,如何减少这个条件?或编写一个具有吸引力的功能。

if (obj.Language.code == "ru") {
        if (obj.Tariff.typeCalc != 1) {
            if (obj.Price.Discount) {
                t = t + getText(obj.Language.code, 'PriceWithDiscount', [Round(obj.Price.Itog), final_currency]);
            }
            else {
                t = t + getText(obj.Language.code, 'PriceNoDiscount', [Round(obj.Price.Itog), final_currency]);
            }
        }
        else {
            if (obj.Price.Discount) {
                t = t + getText(obj.Language.code, 'PriceWithDiscount', [Round(obj.Price.Itog), obj.Currency.symbol]);
            }
            else {
                t = t + getText(obj.Language.code, 'PriceNoDiscount', [Round(obj.Price.Itog), obj.Currency.symbol]);
            }
        }

设计

if (obj.Language.code == "ru") {
}
else {
}

会经常见面,不想堆积

if (obj.Language.code == "ru") {
            if (obj.Price.Discount) {
                
                t = t + getText(obj.Language.code, 'PriceWithDicountOutCity', [obj.Price.Itog, final_currency, Round(obj.Len.value, final_currency), nextkmprice]);
            }
            else {
                
                t = t + getText(obj.Language.code, 'PriceOutCity', [obj.Price.Itog, final_currency, Round(obj.Len.value, final_currency), nextkmprice]);
            }
        }
        else {
            if (obj.Price.Discount) {
                
                t = t + getText(obj.Language.code, 'PriceWithDicountOutCity', [obj.Price.Itog, obj.Currency.symbol, Round(obj.Len.value, obj.Currency.symbol), nextkmprice]);
            }
            else {
                
                t = t + getText(obj.Language.code, 'PriceOutCity', [obj.Price.Itog, obj.Currency.symbol, Round(obj.Len.value, obj.Currency.symbol), nextkmprice]);
            }
        }

4 个答案:

答案 0 :(得分:0)

利用三元运算符根据条件设置值。然后,您可以摆脱一些冗余代码。您可以将其简化为以下形式:

        if (obj.Tariff.typeCalc != 1) {
            var priceType = obj.Price.Discount ? 'PriceWithDiscount' : 'PriceNoDiscount';
            var currencyType = obj.Language.code == "ru" ? final_currency : obj.Currency.symbol;
            t = t + getText(obj.Language.code, priceType, [Round(obj.Price.Itog), currencyType]);
        }

ES6使用let:

        if (obj.Tariff.typeCalc != 1) {
            let priceType = obj.Price.Discount ? 'PriceWithDiscount' : 'PriceNoDiscount';
            let currencyType = obj.Language.code == "ru" ? final_currency : obj.Currency.symbol;
            t = t + getText(obj.Language.code, priceType, [Round(obj.Price.Itog), currencyType]);
        }

答案 1 :(得分:0)

您可以进行一次外部检查和两个条件检查。

if (obj.Tariff.typeCalc != 1) {
    t += getText(
        obj.Language.code,
        obj.Price.Discount ? 'PriceWithDiscount' : 'PriceNoDiscount',
        [
            Round(obj.Price.Itog),
            obj.Language.code == "ru" ? final_currency : obj.Currency.symbol
        ]
    );
}

对于最后一个问题,您可以预先存储currency并使用变量而不是多个条件语句。

var currency = obj.Language.code == "ru" ? final_currency : obj.Currency.symbol;

t += getText(
    obj.Language.code,
    obj.Price.Discount ? 'PriceWithDicountOutCity' : 'PriceOutCity',
    [
        obj.Price.Itog,
        currency,
        Round(obj.Len.value, currency),
        nextkmprice
    ]
);

答案 2 :(得分:0)

可以将对象与基于语言的功能键一起使用:

function langObj(opts){
    this.tarrif = opts.tarrifType;
    this.symbols = [{
        "au":"$",
        "us":"$"
        // include more
    }];

    this.doStuff = function(args){
        return getText(this.code, (args.discount!=-1)?"Price With Discount":"Price No Discount", [Round(args.itog), this.symbols[args.code] ])
    }
    this.ru = function(args){
        args['code']="ru";
        return this.doStuff(args);
    }

    return this;
}

请记住,我不知道“ Itog”是什么,所以我不能全部做,但是基础很简单。可能会自动使用文本文件中的功能和符号或包含tarrif的内容自动填充对象?同样,我不了解上下文,是的。

答案 3 :(得分:0)

创建一个带有两个参数的函数

function fullText(x,y) {
    if(x!=1){
            if (obj.Price.Discount) {
                t = t + getText(obj.Language.code, 'PriceWithDiscount', [Round(obj.Price.Itog), obj.Currency.symbol]);
            }
            else {
                t = t + getText(obj.Language.code, 'PriceNoDiscount', [Round(obj.Price.Itog), obj.Currency.symbol]);
            }
    }
}

为非ru代码创建另一个功能

fullTextNonRu(x,y){
       if (x) {
            if (y) {
                t = t + getText(obj.Language.code, 'PriceWithDiscount', [Round(obj.Price.Itog), obj.Currency.symbol]);
            }
            else {
                t = t + getText(obj.Language.code, 'PriceNoDiscount', [Round(obj.Price.Itog), obj.Currency.symbol]);
            }
        }
  }

使用开关的大小写取决于obj.Language.code

switch(obj.Language.code){
    case "ru":
        fullText(obj.Tariff.typeCalc ,obj.Price.Discount);
        break;
    default:
        fullTextNonRu(obj.Tariff.typeCalc ,obj.Price.Discount);
        break;
 }