Home > Web Front-end > JS Tutorial > js code optimization bits and pieces of records_javascript skills

js code optimization bits and pieces of records_javascript skills

WBOY
Release: 2016-05-16 17:56:12
Original
1081 people have browsed it
1. A simple example is as follows:
switch use case
Copy code The code is as follows:

var caseContent = '';//Processing content after conditional judgment
var caseValue = 5;//Conditional judgment value
switch(caseValue){
case 0:
caseContent = "Shoes";
break;
case 1:
caseContent = "Pants";
break;
case 2:
caseContent = "Coat";
break;
... ...
case 5:
caseContent = "hat";
break;
default :
caseContent = "whatever";
break;
}

Array use case
Copy code The code is as follows:

var caseContent = '';//Conditional judgment post-processing content
var caseValue = 5;//Conditional judgment value
var caseContentArr = ["Shoes", "Pants", "Coat"... ... ,"hat"];
caseContent = caseContentArr[caseValue] ? caseContentArr[caseValue]:"whatever";

2. Analysis of advantages and disadvantages
Array method The code is concise and efficient, but its readability is not as good as the switch use case.
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template