你能帮我简化这个IF结构吗?这是类似javascript的节点,但是专有的东西,所以忽略db.execute的东西:)
if(!("Division" in Shipping))
{
var shipError = false;
Shipping.Division = Billing.Division;
if(!Shipping.Division)
{
if(Shipping.PostalCode)
{
Shipping.Division = Db.ExecuteScalar("SELECT Code from Location.Division D JOIN Location.PostalCode P ON DivisionId = D.ID AND PostalCode=?", Shipping.PostalCode);
if(!Shipping.Division) shipError = true;
}
else
shipError = true;
}
if(shipError){
Errors.push({Code : "SHIPPINGDIVISION", Message : "Shipping State Required"});
Shipping.Division = "";
}
}
答案 0 :(得分:3)
if (!Shipping.Division) {
Shipping.Division = Billing.Division;
}
if (!Shipping.Division && Shipping.PostalCode) {
Shipping.Division = Db.ExecuteScalar(...);
}
if (!Shipping.Division) {
Errors.push({Code : "SHIPPINGDIVISION", Message : "Shipping State Required"});
}
请阅读:
答案 1 :(得分:2)
您不需要shipError
变量,最后可以再次测试if(!Shipping.Division)
。这样可以消除变量声明(显然),但也会删除if
和else
只能设置shipError = true
的声明。结束如下:
if(!("Division" in Shipping)) {
Shipping.Division = Billing.Division;
if(!Shipping.Division) {
if(Shipping.PostalCode) {
Shipping.Division = Db.ExecuteScalar("SELECT statement here",
Shipping.PostalCode);
}
}
if(!Shipping.Division){
Errors.push({Code : "SHIPPINGDIVISION", Message : "Shipping State Required"});
Shipping.Division = "";
}
}
答案 2 :(得分:0)
您是否尝试过使用谷歌关闭?
这是一个javascript优化器。如果您喜欢可以在代码中使用它的一些优化,请尝试一下并查看它如何优化您的代码。
从页面。
JavaScript优化器 Closure Compiler将JavaScript编译为紧凑的高性能代码。编译器删除死代码并重写并最小化剩下的内容,以便下载并快速运行。它还检查语法,变量引用和类型,并警告常见的JavaScript陷阱。这些检查和优化可帮助您编写错误较少且易于维护的应用程序。您可以将编译器与Closure Inspector一起使用,这是一个Firebug扩展,可以调试混淆代码,就像调试人类可读源一样简单。
您的代码已经过优化。
if(!("Division" in Shipping)) {
var shipError = !1;
Shipping.Division = Billing.Division;
if(!Shipping.Division) {
Shipping.PostalCode ? (Shipping.Division = Db.ExecuteScalar("SELECT Code from Location.Division D JOIN Location.PostalCode P ON DivisionId = D.ID AND PostalCode=?", Shipping.PostalCode), Shipping.Division || (shipError = !0)) : shipError = !0
}
if(shipError) {
Errors.push({Code:"SHIPPINGDIVISION", Message:"Shipping State Required"}), Shipping.Division = ""
}
}
;