优化IF条件

时间:2011-06-01 13:28:17

标签: javascript

  AddPatient = {};
  if(GenderValue === undefined) {
        AddPatient.Gender = ' ';  
    } else {
        AddPatient.Gender = GenderValue;  
    }
    if(DateOfBirthValue === undefined) {
        AddPatient.DateOfBirth = ' ';  
    } else {
        AddPatient.DateOfBirth = DateOfBirthValue;  
    }
    if(SSNValue === undefined) {
        AddPatient.SSN = ' ';  
    } else {
        AddPatient.SSN = SSNValue;  
    }
    if(RaceValue === undefined) {
        AddPatient.Race = ' ';  
    } else {
        AddPatient.Race = RaceValue;  
    }
    if(ReligionValue === undefined) {
        AddPatient.Religion = ' ';  
    } else {
        AddPatient.Religion = ReligionValue;  
    }
    if(CellPhoneValue === undefined) {
        AddPatient.CellPhoneNumber1 = ' ';  
    } else {
        AddPatient.CellPhoneNumber1 = CellPhoneValue;  
    }
    if(HomePhoneValue === undefined) {
        AddPatient.phonenumber1 = ' ';  
    } else {
        AddPatient.phonenumber1  = HomePhoneValue;  
    }
    if(PrimaryPhoneValue === undefined) {
        AddPatient.PrimaryPhoneNumber = ' ';  
    } else {
        AddPatient.PrimaryPhoneNumber = PrimaryPhoneValue;  
    }
    if(EmailValue === undefined) {
        AddPatient.EmailAddress1 = ' ';  
    } else {
        AddPatient.EmailAddress1 = EmailValue;  
    }
    AddPatient.ResidentialAddress = {};
    if(AddressValue === undefined) {
        AddPatient.AddressLine1 = ' ';  
    } else {
        AddPatient.ResidentialAddress.AddressLine1 = AddressValue;  
    }
    if(CityValue === undefined) {
        AddPatient.City = ' ';  
    } else {
        AddPatient.ResidentialAddress.City = CityValue;  
    }
    if(StateValue === undefined) {
        AddPatient.State = ' ';  
    } else {
        AddPatient.ResidentialAddress.State = StateValue;  
    }
    if(ZipValue === undefined) {
        AddPatient.PostalCode = ' ';  
    } else {
        AddPatient.ResidentialAddress.PostalCode = ZipValue;  
    }

有没有更好的方法来编写相同的代码?

4 个答案:

答案 0 :(得分:15)

你可以写

AddPatient.Gender = GenderValue || " ";

||运算符返回最左侧的“真实”操作数,因此如果" "为“假”,则评估为GenderValue(例如undefined,{ {1}},false""0

答案 1 :(得分:1)

以下是对您实际问题的建议:

http://jsfiddle.net/mplungjan/b5yRw/

它当然适用于表单。

var GenderValue = "F";
var DateOfBirthValue; // undefined
var AddressValue = "some street";   
// var CityValue;  // undeclared


function addItem(obj,varName) {
  obj[varName] = window.hasOwnProperty(varName+"Value")?window[varName+"Value"]||"not set":"not declared";
}

function addItemLoop(obj) {
  for (var o in obj) {
    if (typeof obj[o] === 'object') {
      addItemLoop(obj[o]);
    }
    else {
      addItem(obj, o);
    }
  }
}

AddPatient = {
  Gender : " ",
  DateOfBirth: " ",
  ResidentialAddress : {
    Address:" ",
    City: " "
  }
}
addItemLoop(AddPatient);

document.write("<br/>Gender (F):"+AddPatient.Gender);
document.write("<br/>DOB (not set):"+AddPatient.DateOfBirth);
document.write("<br/>ResidentialAddress Address (some street):"+AddPatient.ResidentialAddress.Address);
document.write("<br/>ResidentialAddress City (not declared):"+AddPatient.ResidentialAddress.City);

上一个回答

使用三元运算符

不是更好但更短
AddPatient.Gender = (GenderValue === undefined)? " ":GenderValue;  

或快捷方式

AddPatient.Gender = GenderValue || " ";

注意 - 如果值为0,则在快捷方式中将获得空格

如果尚未声明GenderValue,您将收到错误。 所以你需要一个var GenderValue;

示例:

var b="hello", c;
var a = {}
a.x = b||"no b"
a.y = (c===undefined)? "no c here":c
a.z = c || "no c here either" 
alert(a.x)
alert(a.y)
alert(a.z)

答案 2 :(得分:1)

 function isDefined(value){
     if(value != null)
       return value;
     else
      return ' ';
   }

    AddPatient = {};


AddPatient.Gender =isDefined(AddPatient);

和儿子

答案 3 :(得分:0)

这是一个选项:

function SetOrDefault(value, property) {
  if (typeof(value) == 'undefined' || value == null) {
    property = ' ';
  }
  else {
    property = value;
  }
}

然后像这样称呼它:

SetOrDefault(GenderValue, AddPatient.Gender);