The first piece of code is to emphasize this usage, I used a switch in my project, later I found that this kind of code is ugly, so I wrote | | && form, and later, when testing performance, found that performance was on a level of magnitude, it can be seen in some cases to increase performance, but I am not sure what is the situation to improve performance, because I test in the usual case switch and | | && performance is about the same.
The original code:
Copy Code code 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
}
Rewrite the code, the function of course is 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'm chewing is a little more concise, | | && There are many uses to see the introduction of the article
The second piece of code uses an attribute: (Ele=document.createelement ("div");//This expression returns a DOM element that assigns a value to the outside bracket
So come out the following code:
Copy Code code 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'm tired of the whole stack of statements when I build a DOM element:
Copy Code code as follows:
var ele=document.createelement ("div")
Ele.id= "AA";
Ele.classname= "AA"
Ele.innerhtml= "SSS"
Wait, wait, that's annoying.
And then came out of the code above.
The above principle can also write code like this (Ele=document.createelement ("div")). Classname= "AA"; The feeling is not save a little space, above this sentence save a variable name, hehe.