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;