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
WBOYOriginal
2016-05-16 17:56:121044browse
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.
Statement:
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