JavaScript two snippet code, two little tips _javascript tips

Source: Internet
Author: User
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.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.