以正确的方式总结if-loop
Summarize if-loop in the right way
我编写的Javascript代码是为一个小型商店网站的购物车编写的。我知道这段代码非常丑陋,我试着用函数中的参数切换来总结它,但我没有得出一个有效的结论。缩短这段代码的最好方法是什么?
非常感谢!
Javascript:var pack = '#writePackSummary';
var netPriceOutput = '.writeNetPriceSummary';
var taxPriceOutput = '#writeTaxPriceSummary';
var grossPriceOutput = '#writeGrossPriceSummary'
var netPrice1 = 25;
var netPrice3 = 45.55;
var netPrice6 = 89.10;
var tax = 0.19;
if ($(pack).text() == '1') {
$(netPriceOutput).text(netPrice1.toFixed(2).toString().replace(/'./g, ','));
$(taxPriceOutput).text((netPrice1*tax).toFixed(2).toString().replace(/'./g, ','));
$(grossPriceOutput).text(((netPrice1*tax)+netPrice1).toFixed(2).toString().replace(/'./g, ','));
} else if ($(pack).text() == '3') {
$(netPriceOutput).text(netPrice3.toFixed(2).toString().replace(/'./g, ','));
$(taxPriceOutput).text((netPrice3*tax).toFixed(2).toString().replace(/'./g, ','));
$(grossPriceOutput).text(((netPrice3*tax)+netPrice3).toFixed(2).toString().replace(/'./g, ','));
} else if ($(pack).text() == '6') {
$(netPriceOutput).text(netPrice6.toFixed(2).toString().replace(/'./g, ','));
$(taxPriceOutput).text((netPrice6*tax).toFixed(2).toString().replace(/'./g, ','));
$(grossPriceOutput).text(((netPrice6*tax)+netPrice6).toFixed(2).toString().replace(/'./g, ','));
};
对于初学者,您可以用一个函数替换所有这些.toFixed(2).toString().replace(/'./g, ',')
:
function stringNum(num) {return num.toFixed(2).replace(/'./g, ',')}
if ($(pack).text() == '1') {
$(netPriceOutput).text(stringNum(netPrice1));
$(taxPriceOutput).text(stringNum(netPrice1*tax));
$(grossPriceOutput).text(stringNum((netPrice1*tax)+netPrice1));
} etc...
也许可以考虑使用switch
结构而不是多个if-else ?这也更快,因为它不需要每次都重新创建jQuery对象。
先统一逻辑。找到可能被包装成函数的部分,或者使用线性代码并根据选择读取不同的数据。
例如:var pack = '#writePackSummary';
var netPriceOutput = '.writeNetPriceSummary';
var taxPriceOutput = '#writeTaxPriceSummary';
var grossPriceOutput = '#writeGrossPriceSummary'
/* declare the prices as array */
var netPrice = {
1: 25,
2: 45.55,
6: 89.10,
};
var tax = 0.19;
/* read the input only once from the element */
var packNr = parseInt( $( pack ).text() );
/* test if a value dataset exists */
if( netPrice[packNr] ) {
$( netPriceOutput ).text( netPrice[packNr].toFixed(2).toString().replace( /'./g, ',' ) );
$( taxPriceOutput ).text( (netPrice[packNr]*tax).toFixed(2).toString().replace( /'./g, ',' ) );
$( grossPriceOutput ).text( ((netPrice[packNr]*tax)+netPrice[packNr]).toFixed(2).toString().replace( /'./g, ',' ) );
}
这将取代三个if
条件只用一个,并允许快速添加其他价格。
更进一步,识别更多彼此重复的代码部分,并将逻辑组合在可重用的部分(即函数/方法)中。例如《Scimonster》的答案便是"next step"。
相关文章:
- 对于loop.if-仅在经过所有间隔后返回true
- 以这种方式使用if/else-if/else是一种很好的做法
- JavaScript if语句的简洁方式
- Angular 什么是在 ng-repeat 指令中访问总对象的最佳方式
- Javascript Logic In a loop/if statement
- Knockout Js 的渲染方式与基于 Json 值的 if 数据绑定不同
- 编写 if 语句的另一种方式
- 是否有一个jQuery函数来“快捷方式”if/else开关
- Prototype Library for if loop JavaScript
- 我可以以某种方式在开关中使用 if 语句来添加更多案例吗?
- 当IF满足要求时,停止Do While LOOP
- 剔除条件绑定(但不是本机的“if”方式)
- 如何写这段代码的最佳方式,这在javascript中有这么多的if条件
- 需要一种优雅的方式来根据输入改变一行if语句的返回结果
- 重构复杂if表达式的最佳方式
- 编写单行if语句的最佳方式
- 如何用insertAfter和insertBefore代码以三进制方式转换if块
- 有困难的“;对于“;loop+”;setTimeout”;思维方式
- javascript/google map api if statement inside for loop
- 以正确的方式总结if-loop