我有一个对象,data
可能包含也可能不包含成员site_with_same_coords
和/或site_with_same_name
。我对这些进行测试,如果其中一个或两者都存在,我会向用户发出警报:
if (data.site_with_same_coords){
var SameCoordsExists = true;
same_coords_message = 'The site ';
same_coords_message += data.site_with_same_coords.name;
same_coords_message += 'already already exists in the location you have indicated';
}
if (data.site_with_same_name){
var SameNameExists = true;
same_name_message = 'The site ';
same_name_message += data.site_with_same_name.name;
same_name_message += 'already already exists in a differnt location from the one you have indicated';
}
if (SameCoordsExists && SameNameExists){
if(data.site_with_same_name.id != data.site_with_same_coords.id){
alert(same_coords_message + '\n' + same_name_message);
}else if (SameCoordsExists){
alert(same_coords_message);
}
}else if (SameNameExists){
alert(same_name_message);
}
}
有没有更好的方法来做到这一点?