Home >Web Front-end >JS Tutorial >Wrong usage of if...else..
1. Recently, when I was writing js code to complete a function that operates the previous DOM, I mistakenly used the if..else.. control body. Why is it wrong? Take a look at my
code and you will understand:
document.getElementsByClassName('eButton')[0].onclick=function(){ var checked=document.getElementsByClassName('checked'); var eButton=document.getElementsByClassName('eButton')[0]; if(checked.length==0){ alert('请选择要编辑的联系人!'); }else{ if(checked.length >2){ alert('每次编辑只能选择一条记录'); }else{ if(checked[0].childNodes[0].id=='check-all'){ var email=checked[1].parentNode.nextElementSibling.nextElementSibling.innerHTML; var name=checked[1].parentNode.nextElementSibling.innerHTML; document.getElementsByClassName('edit_contact_name')[0].value=name; document.getElementsByClassName('edit_contact_email')[0].value=email; var group=checked[1].parentNode.nextElementSibling.nextElementSibling.nextElementSibling if(group.innerHTML !="default"){ var group_id=group.getAttribute('group_id') document.getElementsByClassName('edit_contact_group_name')[0].value=id; } $('#edit_contact').modal(); }else{ if(checked.length ==2){ alert('每次编辑只能选择一条记录'); }else{ var email=checked[0].parentNode.nextElementSibling.nextElementSibling.innerHTML; var name=checked[0].parentNode.nextElementSibling.innerHTML; document.getElementsByClassName('edit_contact_name')[0].value=name; document.getElementsByClassName('edit_contact_email')[0].value=email; var group=checked[0].parentNode.nextElementSibling.nextElementSibling.nextElementSibling if(group.innerHTML !="default"){ var group_id=group.getAttribute('group_id') document.getElementsByClassName('edit_contact_group_name')[0].value=id; } $('#edit_contact').modal(); } } } } };
Haha, seeing the codes I wrote, I suddenly felt that this function must be very powerful. The whole function was messed up by if..else.., and I started to think about why I used if..else
. In fact, I just considered that there are about three or four different conditions in this logic, and three or four operations need to be performed. , and only one situation can be executed at a time (go to the end of the function after execution), these are the execution flow of the program. With this process in place, I began to think about how to change the code so that it would be clear at a glance and easy to maintain. At this time, I naturally thought of the keyword "return". After using "return", I got the following code:
document.getElementsByClassName('eButton')[0].onclick=function(){ var checked=document.getElementsByClassName('checked'); var eButton=document.getElementsByClassName('eButton')[0]; if(checked.length==0){ alert('请选择要编辑的联系人!'); return; } if(checked.length ==1){ var email=checked[0].parentNode.nextElementSibling.nextElementSibling.innerHTML; var name=checked[0].parentNode.nextElementSibling.innerHTML; document.getElementsByClassName('edit_contact_name')[0].value=name; document.getElementsByClassName('edit_contact_email')[0].value=email; var group=checked[0].parentNode.nextElementSibling.nextElementSibling.nextElementSibling if(group.innerHTML !="default"){ var group_id=group.getAttribute('group_id') document.getElementsByClassName('edit_contact_group_name')[0].value=id; } $('#edit_contact').modal(); return; } if(checked.length ==2){ if(checked[0].childNodes[0].id=='check-all'){ var email=checked[1].parentNode.nextElementSibling.nextElementSibling.innerHTML; var name=checked[1].parentNode.nextElementSibling.innerHTML; document.getElementsByClassName('edit_contact_name')[0].value=name; document.getElementsByClassName('edit_contact_email')[0].value=email; var group=checked[1].parentNode.nextElementSibling.nextElementSibling.nextElementSibling if(group.innerHTML !="default"){ var group_id=group.getAttribute('group_id') document.getElementsByClassName('edit_contact_group_name')[0].value=id; } $('#edit_contact').modal(); return; } alert('每次编辑只能选择一条记录'); return; } if(checked.length >2){ alert('每次编辑只能选择一条记录'); } };
The code will be much clearer when the following conditions are listed:
1.checked.length = =0
2.checked.length ==1
3.checked.length ==2
4.checked.length > 2
Next, encapsulate the duplicate code into a function (remove duplication) and get Code:
function get_edit_modal_content(node_num,checked){ var email=checked[node_num].parentNode.nextElementSibling.nextElementSibling.innerHTML; var name=checked[node_num].parentNode.nextElementSibling.innerHTML; var contact_id=checked[node_num].parentNode.nextElementSibling.getAttribute('contact_id') var regular_name=/[\u4E00-\u9FA5\uF900-\uFA2D\w]*/; name=regular_name.exec(name)[0]; document.getElementsByClassName('edit_contact_id')[0].value=contact_id; document.getElementsByClassName('edit_contact_name')[0].value=name; document.getElementsByClassName('edit_contact_email')[0].value=email; var group=checked[node_num].parentNode.nextElementSibling.nextElementSibling.nextElementSibling if(group.innerHTML !="default"){ var group_id=group.getAttribute('group_id') document.getElementsByClassName('edit_contact_group_name')[0].value=group_id; } } document.getElementsByClassName('eButton')[0].onclick=function(){ var checked=document.getElementsByClassName('checked'); if(checked.length==0){ alert('请选择要编辑的联系人!'); return; } if(checked.length==1){ get_edit_modal_content(0,checked); $('#edit_contact').modal(); return; } if(checked.length==2){ if(checked[0].childNodes[0].id=='check-all'){ get_edit_modal_content(1,checked); $('#edit_contact').modal(); return; } alert('每次编辑只能选择一条记录'); return; } if(checked.length >2){ alert('每次编辑只能选择一条记录'); return; } };