Javascript programming Specification

Source: Internet
Author: User
Situ zhengmei drafted the javascript programming specifications for the company, referring to a lot of information on the Internet, especially the google specifications. Now, I hope you can give me some valuable comments. This specification is developed based on the reality that javascript functional programming styles and companies rely heavily on jQuery for coding. Disable SyntaxHighlighter

Situ zhengmei

Many documents on the Internet, especially the google specifications, have been referenced for the javascript programming specifications drafted by the company. Now, I hope you can give me some valuable comments.

This specification is developed based on the reality that javascript functional programming styles and companies rely heavily on jQuery for coding.

Disable the use of eval, with, and caller (use strict requirements of ecma262 v5 ). Eval can only be generated by machines during encryption.


The var keyword must be added to declared variables. In principle, continuous declarations are not allowed except in statements such as for (;) loops. Continuous declarations are less efficient than separate declarations, and they are easily exposed to the global scope by mistake.

Constant, all uppercase.

Variable name, which cannot use Pinyin. English words are organized in the camper style.

Semicolons (;) are added to all places that can be added (except for, function, if, switch, try, while) to prevent compression failure due to this problem.

Custom classes can only be used to build the UI library, and the Business Code is not allowed to define classes without permission.

In principle, pseudo objects (String, Number, and Boolean) cannot be used directly.

In principle, Array and Object are not allowed, and they are directly [], {}.

Handle this with caution to prevent binding from being invalid and point to the window. We recommend that you use that to reference it.

If you want to call the function itself, you are not allowed to use the name function expression. Write the following code in the first line of the target function. For details, refer to google naming function expressions:

Var self = arguments. callee;

Do not extend native Object prototypes, especially Object. prototype.

Do not bind events to an element on the page, that is, the following code style:

Jump to a page

Disable the use of IE conditional comments, and no compression is allowed.

Var f = function (){

/* @ Cc_on if (@ _ jscript) {return 2 * @ */3 ;/*@}@*/

};

Block Function Declaration prohibited. For details, please google name function expression secrets.

If (x) {// ng

Function foo (){}

}

If (x ){

Var foo = function (){}

}

The for-in loop can only be used for objects.

Multi-line string is prohibited, because the blank characters at the starting position of the line cannot be ignored during compilation; the blank characters after "" will produce odd errors; although most script engines support this method, it is not the standard specification of ECMAScript.

Var myString = A rather long string of English text, an error message

Actually that just keeps going and going -- an error

Message to make the Energizer bunny blush (right through

Those Schwarzenegger shades )! Where was I? Oh yes,

Youve got an error and all the extraneous whitespace is

Just gravy. Have a nice day .;

In principle, String concatenation of HTML code is not allowed. Use the foreground template or background template.

String literal, which is better ".

Note: Use JSDoc.

Each line should not be too long. After writing a piece of code, use IDE to format it.

Add custom variables to the element and use the "data-" prefix in a uniform manner, so that the prefix is connected to the "data-*" mechanism of html5.

Prohibiting jQuery from using more than one row of chain operations is very difficult to read.

Select an element. Use ID selector, class selector, and label selector. Use the sub-element structure pseudo class and position pseudo class with caution (nth-child,: first,: eq,: gt)

Search for nearby element nodes on an existing jQuery object. We do not recommend that you use multi-level find instead of related traversal functions.

JavaScript programs should be stored in external JS files as much as possible for compression and caching.

Standard features are better than non-standard features (if a class library is provided, use the functions in the class library first ).


$ ("XXXX"). find ("YYYY"). find ("ZZZZ"); // ng

$ ("XXXX"). next () // or nextUntil, nextAll, prev, prevAll, prevUntl, children, closest,

. Siblings

When adding events to an element, the order of consideration is delegate> live> bind.

JQuery has a problem in the following events: change resize mouseenter mouseleave mousewheel. Generally, event proxies are not used. For example, mousewheel events can only be implemented by plug-ins.


Leave no future code snippets in the JS File

Any ID or class name referenced by jQuery should use js _ as the prefix to warn others to accidentally delete it when adjusting the style.

JavaScript code must be placed in the $ namespace object. The execution of all functions starts from the main function.

; $ (Function (){

//... Other variables used

Var $ = window. $ = {

// This page provides the private helper function 1.

_ Partition 1: function (){

},

// Secondary function 2 Private on this page

_ Limit 2: function (){

},

// Third-party Auxiliary Function

_ Arguments 3: function (){

},

// Secondary function 4 private on this page

_ Limit 4: function (){

},

// Auxiliary function 5 private to this page

_ Limit 5: function (){

},

//... More private functions

// Function 1

Feature1: function (){

},

// Function 2

Feature2: function (){

},

// Function 3

Feature3: function (){

},

// Function 4

Feature4: function (){

},

// Function 5

Feature5: function (){

},

// The JSON data obtained from the background is stored in this object for other function calls.

Jsons :{},

//... More requirements. One requirement corresponds to one function.

Main: function (){

$. Feature1 ();

$. Feature2 ();

$. Feature3 ();

$. Feature4 ();

//... Call them in the main function.

}

& Nb

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.