Home Web Front-end JS Tutorial Refactoring Javascript code examples (comparison before and after refactoring)_javascript skills

Refactoring Javascript code examples (comparison before and after refactoring)_javascript skills

May 16, 2016 pm 05:43 PM
Refactor

Today I made several tutorial articles on asp.net combined with Javascript. Now let's look back at those Javascript scripts. Some of them are not ideal and too complicated. Now extract them and reconstruct them.
The previous one:

Copy the code The code is 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 after reconstruction Script:
Copy code The code is 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;":"";
}

Previous two:
Copy code The code is 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;
}
}
}

Refactored Javascript script:
Copy code The code is 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;
}
}
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the code complexity error in Python code? How to solve the code complexity error in Python code? Jun 24, 2023 pm 05:43 PM

Python is a simple, easy-to-learn and efficient programming language, but when we write Python code, we may encounter some problems with excessive code complexity. If these problems are not solved, it will make the code difficult to maintain, error-prone, and reduce the readability and scalability of the code. So, in this article, we will discuss how to resolve code complexity error in Python code. Understanding Code Complexity Code complexity is a measure of the nature of code that is difficult to understand and maintain. In Python, there are some indicators that can be used

How to solve the poor maintainability error of Python code? How to solve the poor maintainability error of Python code? Jun 25, 2023 am 11:58 AM

Python, as a high-level programming language, is widely used in software development. Although Python has many advantages, a problem that many Python programmers often face is that the maintainability of the code is poor. The maintainability of Python code includes the legibility, scalability, and reusability of the code. In this article, we will focus on how to solve the problem of poor maintainability of Python code. 1. Code readability Code readability refers to the readability of the code, which is the core of code maintainability.

How to refactor Java code well How to refactor Java code well Jun 15, 2023 pm 09:17 PM

As one of the most popular programming languages ​​in the world, Java has become the language of choice for many businesses and developers. However, code refactoring is crucial to maintaining code quality and development efficiency. Java code can become increasingly difficult to maintain over time due to its complexity. This article will discuss how to refactor Java code to improve code quality and maintainability. Understand the principles of refactoring The purpose of Java code refactoring is to improve the structure, readability and maintainability of the code, rather than simply "changing the code". because

In-depth understanding of function refactoring techniques in Go language In-depth understanding of function refactoring techniques in Go language Mar 28, 2024 pm 03:05 PM

In Go language program development, function reconstruction skills are a very important part. By optimizing and refactoring functions, you can not only improve code quality and maintainability, but also improve program performance and readability. This article will delve into the function reconstruction techniques in the Go language, combined with specific code examples, to help readers better understand and apply these techniques. 1. Code example 1: Extract duplicate code fragments. In actual development, we often encounter reused code fragments. At this time, we can consider extracting the repeated code as an independent function to

Optimization and refactoring methods in Go language Optimization and refactoring methods in Go language Jun 02, 2023 am 10:40 AM

Go language is a relatively young programming language. Although from the design of the language itself, it has taken into account many optimization points, making it efficient in performance and good maintainability, this does not mean that we are developing Go applications do not require optimization and refactoring. Especially in the long-term code accumulation process, the original code architecture may have begun to lose its advantages. Optimization and refactoring are needed to improve the performance and maintainability of the system. This article will share some optimization and refactoring methods in Go language, hoping to be helpful to Go developers

React code refactoring guide: How to improve the code structure and readability of front-end applications React code refactoring guide: How to improve the code structure and readability of front-end applications Sep 26, 2023 am 08:37 AM

React code refactoring guide: How to improve the code structure and readability of front-end applications. In front-end development, code structure and readability are crucial to the maintenance and expansion of the project. As the project scale gradually increases and the code becomes more complex, we need to refactor the code to better organize the code and improve maintainability and readability. This article will introduce how to refactor React code from the following aspects and provide specific code examples. 1. Component splitting In React development, splitting into smaller components is an effective way to refactor code.

How to refactor code in Go language How to refactor code in Go language Jun 02, 2023 am 08:31 AM

With the continuous deepening of software development and the continuous accumulation of code, code refactoring has become an inevitable part of the modern software development process. It is a process of modifying the established code of a system to improve its structure, performance, readability, or other related aspects. In this article, we will explore how to do code refactoring in Go language. Defining Refactoring Goals Before starting code refactoring, we should set a clear refactoring goal. We need to ask ourselves some questions, such as what are the problems with this code? We need to reconstruct

Code refactoring experience and suggestions in Java development Code refactoring experience and suggestions in Java development Nov 22, 2023 pm 08:12 PM

In Java development, code refactoring is a very important link. It can help us improve code quality, reduce code redundancy, and improve maintainability and performance. This article will share some experiences and suggestions on code refactoring, hoping to be helpful to Java developers. Determine the goals of refactoring Before refactoring code, you first need to clarify what the goals of refactoring are. Is it to improve code readability? Or is it to improve performance? Or is it to fix bugs? Clear goals can help us refactor in a more targeted manner instead of blindly making changes.

See all articles