Refactoring JavaScript code samples (compare before and after refactoring) _javascript tips

Source: Internet
Author: User
Today there are a few asp.net with JavaScript tutorial articles. Now look at those JavaScript scripts that are not well written and overly complex. Now extract them and refactor them.
before a
Copy Code code as follows:

function Selectedall (CB) {
cb.checked = cb.checked? False:true;
var gv = document.getElementById (' <%=gridviewcouplets.clientid%> ');
var rc = gv.rows.length;
for (var i = 1; i < RC; i++) {
var input = gv.rows[i].cells[0].getelementsbytagname ("input");
if (Input[0].type = = "checkbox" && input[0].checked) {
input[0].checked = false;
Gv.rows[i].style.backgroundcolor = "";
}
else {
Input[0].checked = true;
Gv.rows[i].style.backgroundcolor = "#66ff33;";
}
}
}
function Selectedsingle (CB) {
var row = Cb.parentNode.parentNode;
if (cb.checked) {
Row.style.backgroundColor = "#66ff33;";
}
else {
Row.style.backgroundColor = "";
}
}

JavaScript script after refactoring:
Copy Code code as follows:

function Selectedall (CB) {
var gv = document.getElementById (' <%=gridviewcouplets.clientid%> ');
var rc = gv.rows.length;
for (var i = 1; i < RC; i++) {
var input = gv.rows[i].cells[0].getelementsbytagname ("input");
if (Input[0].type = "checkbox")
{
input[0].checked = cb.checked;
Gv.rows[i].style.backgroundcolor = input[0].checked? "#66ff33;": "";
}
}
}
function Selectedsingle (CB) {
var row = Cb.parentNode.parentNode;
Row.style.backgroundColor = cb.checked? "#66ff33;": "";
}

before two
Copy Code code as follows:

function Check_uncheck_all (CB) {
var cbl = document.getElementById ("<%=CheckBoxListMusicType.ClientID%>");
var input = cbl.getelementsbytagname ("input");
if (cb.checked) {
for (var i = 0; i < input.length; i++) {
Input[i].checked = true;
}
}
else {
for (var i = 0; i < input.length; i++) {
input[i].checked = false;
}
}
}

JavaScript script after refactoring
Copy Code code as follows:

function Check_uncheck_all (CB) {
var cbl = document.getElementById ("<%=CheckBoxListMusicType.ClientID%>");
var input = cbl.getelementsbytagname ("input");
for (var i = 0; i < input.length; i++) {
input[i].checked = cb.checked;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.