The learning of JavaScript stingy code _javascript skills

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