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:
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:
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:
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.