2011-06-01 77 views
3
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; 
    } 

有沒有更好的方法來編寫相同的代碼?優化IF條件

+2

稍微好一點: 'AddPatient.Gender = GenderValue || '';' 等等。 – 2011-06-01 13:32:14

+0

請參閱我的更新,以獲得半自動版本 – mplungjan 2011-06-02 18:16:59

回答

15

你可以寫

AddPatient.Gender = GenderValue || " "; 

||運算符返回最左邊的 「truthy」 操作,所以這將評估爲" "如果GenderValue是 「falsy」(如undefinedfalse""0,或null

+0

如此美觀和整潔。 – Patricia 2011-06-01 13:30:56

+0

+1,絕對是這樣做的Javascript方式。 – Exelian 2011-06-01 13:33:02

+0

+1,這很不錯,但我也對更優雅的方式感興趣,優先考慮更自動。也許基於映射一系列參數的東西? – 2011-06-01 13:38:47

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) 
+0

在那裏丟失= = – epascarello 2011-06-01 13:31:32

+0

不,我不:) :) – mplungjan 2011-06-01 13:32:15

+0

因爲有人編輯了他們的答案,並增加了更多基於其他用戶的評論?:) – epascarello 2011-06-01 13:34:37

0

這裏有一個選項:

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

然後調用它像這樣:

SetOrDefault(GenderValue, AddPatient.Gender); 
+1

這完全不起作用。 (引用是按值傳遞的) – SLaks 2011-06-01 13:34:50

+0

@SLaks,你的意思是:基元是按值傳遞的? – 2011-06-01 13:44:01

+0

@Max:No;我的意思是對象的引用是按值傳遞的。 – SLaks 2011-06-01 13:44:37

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

    AddPatient = {}; 


AddPatient.Gender =isDefined(AddPatient); 

等n on