Normative thoughts in JavaScript development

Source: Internet
Author: User

Once and for all, do not duplicate the wheel :)
1. Uniform placement of common methods
For example, when registering a user, you often need to determine whether the characters in the text box are Chinese characters, English letters, numbers, or email addresses. Why not put these methods in a single script named utility. js?
Copy codeThe Code is as follows:
// Save it as a js file as needed

Function isNull (obj)
{
If (! Obj | obj. length = 0 | obj = "")
{
Parent. MyAlert ("the annotation name cannot be blank! ", AlertImg );
Return false;
}
Else
{
Return true;
}
}

// Verify whether it is an integer
Function isNumber (oNum)
{
If (! ONum) return false;

Try {
If (parseInt (oNum )! = ONum ){
Parent. MyAlert ("enter a positive integer for the query distance! ", AlertImg );
Return false;
}
}
Catch (ex)
{
Parent. MyAlert ("enter a positive integer for the query distance! ", AlertImg );
Return false;
}

Return true;
}

// Verify whether it is Chinese
Function isChinese (oCn)
{
If (! OCn | oCn. length = 0) return false;

Try {
Var reg =/^ [\ u4e00-\ u9fa5] + $/I; // Chinese

If (reg. test (oCn ))
{
Return true;
}
Else {
Parent. MyAlert ("enter Chinese keywords! ", AlertImg );
Return false;
}
}
Catch (ex)
{
Parent. MyAlert ("enter Chinese keywords! ", AlertImg );
Return false;
}
}

// Verify whether it is a Chinese character or a letter
Function isEnCh (oStr)
{
If (! OStr | oStr. length = 0) return false;

Try {
Var reg =/^ [a-zA-Z \ u4E00-\ u9FA5]/g; // contains Chinese or pinyin

If (reg. test (oStr ))
{
Return true;
}
Else {
Parent. MyAlert ("enter Chinese or pinyin for place names! ", AlertImg );
Return false;
}
}
Catch (ex)
{
Parent. MyAlert ("enter Chinese or pinyin for place names! ", AlertImg );
Return false;
}
}

// The result is retained with 2 decimal places.
Function roundAmount (n ){
Var s = "" + Math. round (n * 100)/100;
Var I = s. indexOf ('.')
If (I <0) return s + ". 00 ";

Var t = s. substring (0, I + 1) + s. substring (I + 1, I + 3 );
If (I + 2 = s. length) t + = "0 ";

Return t;
}

2. Uniform placement of prompts and error messages

After reading the above Code, do you also find a problem: If the prompt and error message are different in the next project, you need to modify the above code again. Why not store the prompt information in a script named resource_zh.js?

In multi-language software, similar tricks are also used to switch the interface language? Haha.
Copy codeThe Code is as follows:
Var page_res = {
"Meter": "meters ",
"Kilometer": "km )",
"Mile": "Miles ",
"Yard": "code ",
"Degree": "degree ",
"Millimeter": "millimeters ",
"SaveMap": "Save map ",
"PrintMap": "print map ",
"QueryResult": "query result ",
"QueryResultNull": "The query result is empty ",
"ExperssionCanNotNull": "The query expression cannot be blank ",
"NetworkInfo": "path information ",
"ArcInfo": "arc section information ",
"AddEntitySuccess": "added! ",
"AddEntityFail": "An error occurred while adding the ground map! ",
"UpdateEntitySuccess": "The terrain is updated successfully! ",
"UpdateEntityFail": "An error occurred while updating the ground map! ",
"UpdatePropertySuccess": "attributes updated successfully! ",
"UpdatePropertyFail": "An error occurred while updating the property! ",
"DeleteEntitySuccess": "The object is deleted successfully! ",
"DeleteEntityFail": "An error occurred while deleting the object! ",
"ClosestFacilityFail": "facility analysis failed recently. Please select another one ",
"FieldCanNotNull": "The number of fields cannot be blank ",
"FieldMustInteger": "The number of fields must be an integer ",
"FieldMustMoreThanZero": "The number of fields must be greater than 0 ",
"NumberCanNotNull": "The value cannot be blank"
}

We can place common unit, button text, and prompt information here. Then, load the script first and generate an object through the eval instance. You can obtain the corresponding content through res. networkInfo.

3. AjaxRequest request Encapsulation

It is consistent with the first point. Ajax is frequently used in current development. If you do not use jQuery or other script libraries, you may write the functions for each request and callback by yourself. Why not put all these methods in an ajax. js?
Copy codeThe Code is as follows:
Var xmlhttpObj = false;
Function XHR (CallBack)
{
This. callback = CallBack;
}
XHR. createXMLHttp = function ()
{
If (window. ActiveXObject) // IE browser
{
Try
{
XmlhttpObj = new ActiveXObject ("Microsoft. XMLHTTP"); // IE4.0
}
Catch (e)
{
Try
{
XmlhttpObj = new ActiveXObject ("Msxml2.XMLHTTP"); // IE5.0 or above
}
Catch (e2)
{
XmlhttpObj = false;
}
}
}
Else if (window. XMLHttpRequest &&! XmlhttpObj) // open a browser
{
XmlhttpObj = new XMLHttpRequest ();
}
}
XHR. prototype. Onstar = function (method, Url, bFlag, param)
{
If (this. callback! = Null)
{
XHR. createXMLHttp ();
XmlhttpObj. onreadystatechange = this. callback;
XmlhttpObj. open (method, Url, bFlag );
XmlhttpObj. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded ;");
XmlhttpObj. send (param );
}
Else
{
Alert ("no client processing function! ")
}
}

Instantiate an XHR object during use, for example, var legendObj = new XHR (function (){...}); then use legendObj. onstar ("POST", "Handlers/legendHandler. ashx ", false," mapName = "+ mapName); Submit the request.

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.