1, stingy your code, with the least code to do the most appropriate thing;
For example, your code uses a lot of document.getElementById (), do you consider writing a simple ID selector
Copy Code code as follows:
function $ (Id)
{
return document.getElementById (ID);
}
2, stingy your patch, do not in order to achieve the function of the existing framework on the patch, but the framework of the implementation of the extension, if the framework can not expand, whether to consider partial refactoring?
For example, you already have a complete set of tables Tanzhong the validation framework, one day you find that a form in a serious frame can not find the corresponding regular, you may do is to append an if to the simple implementation, but why not within the validation framework to extend a regular, keep the code clean?
, stingy code execution steps
For example, when we write Ajax code, we often write the following code:
Copy Code code as follows:
var xmlobject;
function Createxmlhttprequest ()
{
if (window. ActiveXObject)
{
Xmlobject = new Activeobject ("Microsoft.XMLHTTP");
}
Else
{
Xmlobject = new XMLHttpRequest ();
}
}
But every time we generate an object we have to make a judgment, why not remember it after the first generation of the object, next time directly to new? After the improvement, the following
Copy Code code as follows:
var _ajax = function () {
_self = this;
}
_ajax.prototype = {
/**
* Building HTTP Request objects
*/
_create:function () {
var factories = [
function () {return new XMLHttpRequest ();},//Non IE series
function () {return new ActiveXObject ("Microsoft.XMLHTTP");},//ie
function () {return new ActiveXObject ("Msxml2.xmlhttp");}//ie some versions
];
for (var i = 0; i < factories.length; i++) {
try {
if (Factories[i] ()) {
return factories[i];
}
}
catch (e) {
Continue
}
}
return factory[2];
}(),
}
This code looks much more than the previous steps, but after the first call to _ajax._create (), _ajax._create has been changed to an anonymous function compatible with the current browser, and subsequent calls are no longer judged;