Summary of javascript code specifications

Source: Internet
Author: User

1. Javascript code should comply with Douban-JSLint test standards

1-1. The statement must end with a semicolon, except for, function, if, switch, try, while

1-2. Only long statements can be considered to be disconnected, for example:

TEMPL_SONGLIST.replace ('{TABLE}', da ['result'])
. Replace ('{PREV_NUM}', prev)
. Replace ('{NEXT_NUM}', next)
. Replace ('{CURRENT_NUM}', current)
. Replace ('{TOTAL_NUM}', da. page_total );

To avoid conflicts with the JSLint validation mechanism, operators such as "." or "+" are placed at the end of the line. The code above should be changed:

TEMPL_SONGLIST.replace ('{TABLE}', da ['result']).
Replace ('{PREV_NUM}', prev ).
Replace ('{NEXT_NUM}', next ).
Replace ('{CURRENT_NUM}', current ).
Replace ('{TOTAL_NUM}', da. page_total );

1-3. Avoid extra commas. For example, var arr = [1, 2, 3,];

1-4. All cyclic bodies and judgment bodies must be included in. For example:

Error:

If (condition)
Statement;
Or
If (condition) statement;

Right:

If (condition ){
Statement; or
If (condition) {statement ;}

1-5. in the for-in loop body, you must use the hasOwnProperty method to check whether the member is a member of itself. Avoid contamination from the prototype chain.

1-6. Variable declaration. The variable declaration should be placed at the top of the function. Avoid using undeclared variables.

Error:

If (n> 0 ){
Var isvalid = true;

Right:

Var isvalid;
If (n> 0 ){
Isvalid = true;
}

1-7. Do not use with, void, or edevil.

1-8. Use a strict condition operator. Use = instead of =, use! = Replace! =.

1-9. the following types of objects are not recommended to be constructed using new: new Number, new String, new Boolean, new Object (replaced by {}), and new Array (replaced ).

1-10. The reference object member uses obj. prop1 to replace obj ["prop1"] unless the attribute name is a variable.

Note: Douban-JSLint is a customized JSLint.

Note: If you want to skip the JSLint check by using other global variables in the module code, you can add a declaration to the file, such:

2. Javascript naming rules

2-1. uppercase letters of the constructor. For example:

Function Dialog (config ){
Statement;
} Var dlg = new Dialog ({...});

2-2. The property or method name of the object uses the lower camel-case method, such as "init", "bindEvent", "updatePosition ":

Dialog. prototype = {
Init: function (){},
BindEvent: function (){},
UpdatePosition: function (){}};

2-3. The private variable name starts with an underscore. For example, "_ current", "_ defaconfig config"

2-4. All constant names are capitalized and words are separated by underscores. For example, "CSS_BTN_CLOSE" and "TXT_LOADING"

2-5. prefix of variable name:

Prefix

Element

Example

Integer

NVariableName

I, j, k, m, n, etc .*

Integer as counter/iterator

(For I = 0; I <= oArray. length; I ++)

String

SVariableName

Object

OObjectName

Is, can, has

Boolean

[Boolean name] ConditionName

Event method

Event attachment

[Event type] _ MethodName

Accessor method

GetMethodName

Accessor method

SetMethodName

Note: Only a counter/iterator shoshould use a single-letter designation.

3. Code formatting requirements

3-1. Necessary spaces and indentation in the statement

3-1-1. It must be followed by spaces before and after the "()" used to contain the statement, such as: if/for/while/switch (statements ){... }

3-1-2. "=" must be followed by Spaces

3-1-3. "," between array members must be followed by a space

Bad:

For (t in selected) {if (! Hash [t]) deselect (t )}

Good:

For (t in selected ){
If (! Hash [t]) {
Deselect (t );}

3-2. Use a long statement to disconnect rows:

Bad:

TEMPL_SONGLIST.replace ('{TABLE}', da ['result']). replace ('{PREV_NUM}', prev ). replace ('{NEXT_NUM}', next ). replace ('{CURRENT_NUM}', current ). replace ('{TOTAL_NUM}', da. page_total );

Good:

TEMPL_SONGLIST.replace ('{TABLE}', da ['result']).
Replace ('{PREV_NUM}', prev ).
Replace ('{NEXT_NUM}', next ).
Replace ('{CURRENT_NUM}', current ).
Replace ('{TOTAL_NUM}', da. page_total );

3-3. Format object parameters:

Bad:

EmbedSWF (id, {url: '/swf/player30792.swf? Url = '+ el. href, width: 261, height: 30, params: {wmode: 'transparent'}, attributes: {id: "player-sample" + I, name: "player-sample" + I }});

Good:Copy codeThe Code is as follows: embedSWF (id ,{
Url: '/swf/player30792.swf? Url = '+ el. href,
Width: 261,
Height: 30,
Params: {wmode: 'transparent '},
Attributes :{
Id: "player-sample" + I,
Name: "player-sample" + I
});

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.