How to streamline javascript code

Source: Internet
Author: User

1. Compress Your code and use the least code to do the most appropriate thing;
For example, if you use document. getElementById () in your code, do you want to write a simple ID selector?
Copy codeThe Code is as follows:
Function $ (Id)
{
Return document. getElementById (Id );
}

2. Do not patch your patch to implement the function on the existing framework, but write the implementation on the Framework extension. If the framework cannot be expanded, do you want to consider partial refactoring?
For example, if you already have a complete form Regular Expression verification framework, one day you find that a form cannot find the corresponding regular expression in the severe framework, your possible practice is to append an if statement to achieve simple implementation. But why not extend a regular expression in the verification framework to keep the code clean?
Steps for executing code
For example, when writing ajax code, we often write the following code:
Copy codeThe Code is as follows:
Var xmlObject;
Function createXMLHTTPRequest ()
{
If (window. ActiveXObject)
{
XmlObject = new ActiveObject ("Microsoft. XMLHTTP ");
}
Else
{
XmlObject = new XMLHTTPRequest ();
}
}

However, every time an object is generated, we need to make a judgment. Why don't we remember it after the first object is generated? What about new next time? The improvements are as follows:
Copy codeThe Code is as follows:
Var _ ajax = function (){
_ Self = this;
}
_ Ajax. prototype = {
/**
* Construct an http request object
*/
_ Create: function (){
Var factories = [
Function () {return new XMLHttpRequest () ;}, // non-IE Series
Function () {return new ActiveXObject ("Microsoft. XMLHTTP") ;}, // IE
Function () {return new ActiveXObject ("Msxml2.XMLHTTP");} // some versions of IE
];
For (var I = 0; I <factories. length; I ++ ){
Try {
If (factories [I] () {
Return factories [I];
}
}
Catch (e ){
Continue;
}
}
Return factory [2];
}(),
}

This Code seems to be much more than the preceding steps, but when _ ajax is called for the first time. after _ create (), _ ajax. _ create has been changed to an anonymous function compatible with the current browser and will not be judged in future calls;

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.