/ Published in: JavaScript
Expand |
Embed | Plain Text
var type.value = 's'; // bad - redundent if ('s' == type.value) { cont.style.display = 'none'; if (cont.shim) cont.shim.style.display = 'none'; if (cats) cats.style.display = 'none'; } else { cont.style.display = ''; if (cont.shim) cont.shim.style.display = ''; if (cats) cats.style.display = ''; } // good - take the only different var val = type.value === 's' ? 'none' : ''; // the only different cont.style.display = val; if (cont.shim) cont.shim.style.display = val; if (cats) cats.style.display = val; // better - abstract a method var val = type.value === 's' ? 'none' : ''; setDisplay(cont, val); setDisplay(cont.shim, val); setDisplay(cats, val); function setDisplay(el, val) { if(el) el.style.display = val; } // best - take a set of elements setDisplay([cont, cont.shim, cats], type.value === 's' ? 'none' : ''); function setDisplay(els, val) { // loop though the array for (var i = 0, len = els.length, el; i < len && (el = els[i]); i++) { el.style.display = val; } }
You need to login to post a comment.
