Home > Web Front-end > JS Tutorial > body text

Two pieces of javascript code, two tips_javascript tips

WBOY
Release: 2016-05-16 18:35:31
Original
912 people have browsed it

The first piece of code is to emphasize this usage. I used a switch in my project. Later I found that this code was ugly, so I wrote it in the form of ||&&. Later, when I tested the performance, I found that the performance improved. By an order of magnitude, it can be seen that this writing method can increase performance in some cases, but I am not sure under what circumstances it can improve performance, because I tested that the performance of switch and ||&& are almost the same under normal circumstances.
Original code:

Copy code The code is as follows:

switch(this.now_char=this .str.charAt(this.index)){
case "/":
if(this.handleNote()) continue;else this.str2 =this.now_char;
break;
case """:
case "'":
if(this.handleStr()) continue;else this.str2 =this.now_char;
break;
case "n":
if(this.handleLine()) continue;else this.str2 =this.now_char;
break;
case "{":
case "}":
if(this.handleDepth() ) continue;else this.str2 =this.now_char;
break;
case ":":if(this.handleJson()) continue;else this.str2 =this.now_char;break;
default :
if(this.handleKeyword()) continue;else this.str2 =this.now_char;
break;
}

The function of the rewritten code is of course the same view sourceprint?1 (this.now_char=="/"&&(this.handleNote()||(this.str2 =this.now_char)))||
((this.now_char=="""| |this.now_char=="'")&&(this.handleStr()||(this.str2 =this.now_char)))||
(this.now_char=="n"&&(this.handleLine( )||(this.str2 =this.now_char)))|| ((this.now_char=="{"||this.now_char=="}")&&(this.handleDepth()||(this.str2 =this.now_char)))||
(this.handleKeyword()||(this.str2 =this.now_char))
The second way I chew is more concise, there are many more ||&& For usefulness, you can read the introduction of that article
The second piece of code uses a feature: (ele=document.createElement("div"));//This expression will return a dom element, and when assigned, it will Return the value to the outer bracket
and the following code comes out:
Copy the code The code is as follows:

var mixin=function(target,options){
for(var i in options){
target[i]=options[i]
}
}
var ele=null;
mixin(ele=document.createElement("div"),{
id: "aa",
className: "bb",
innerHTML: "sss"
})
document.body.appendChild(ele)
debug(ele.id)//aa
debug(ele.className)//bb
debug(ele.innerHTML)//sss

This code is because I am really tired of a lot of statements when creating a dom element:
Copy code The code is as follows:

var ele=document.createElement("div")
ele.id="aa";
ele.className="aa"
ele.innerHTML="sss"

Wait, wait, etc., so annoying.
So the above code came out.
Using the above principle, you can also write the code like this ( ele=document.createElement("div")).className="aa"; It feels like it saves a little space. The above sentence saves a variable name, haha.
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