Standard JavaScript code specification detailed

Source: Internet
Author: User
Tags hash prev

1. JavaScript code should conform to Douban-jslint inspection standard

1-1. Statements must have a semicolon ending, except for, function, if, switch, try, while

1-2. Only long statements can consider breaking, such as:

The code is as follows Copy Code

Templ_songlist.replace (' {TABLE} ', da[' results '])
. replace (' {prev_num} ', PREV)
. replace (' {next_num} ', NEXT)
. replace (' {current_num} ', current)
. replace (' {total_num} ', da.page_total);

To avoid conflict with the jslint test mechanism, "." or "+" such operators put at the end of the line, the above code should read:

The code is as follows Copy Code

Templ_songlist.replace (' {TABLE} ', da[' results ']).
Replace (' {prev_num} ', PREV).
Replace (' {next_num} ', NEXT).
Replace (' {current_num} ', current).
Replace (' {total_num} ', da.page_total);

1-3. Avoid extra commas. such as: var arr = [1,2,3,];

1-4. All circulation bodies and judgments need to be enclosed in "{}". Such as:

The code is as follows Copy Code

Wrong:

if (condition)
Statement
Or
if (condition) statement;

Right:

if (condition) {
Statement Or
if (condition) {statement;}

The 1-5. For-in loop body must use the hasOwnProperty method to check whether a member is a member of its own. Avoid contamination from the prototype chain.

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

The code is as follows Copy Code

Wrong:

if (n > 0) {
var isvalid = true;

Right:

var IsValid;
if (n > 0) {
IsValid = true;
}

1-7. Do not use with, void, evil.

1-8. The use of strict condition-judging characters. Use = = = instead of = =, replace!= with!==.

1-9. The following types of objects are not recommended for new constructs: New number, new String, new Boolean, new object (with {} instead), New Array (with [] instead).

1-10. Reference object members use OBJ.PROP1 instead of obj["Prop1", unless the property name is a variable.

Note: Douban-jslint is a custom jslint

Note: If you use other global variables to skip JSLint checks in the module code, you can add a declaration to the file, such as:

2. JavaScript naming rules

2-1. The first letter of the constructor is capitalized. Such as:

  code is as follows copy code

                          function Dialog (config) {
                             statement;
                         }                           var dlg = new Dialog ({...});

2-2. The object's properties or method names are used in small hump style (lower camel-case), such as "Init", "bindevent", "updateposition":

The code is as follows Copy Code

                           Dialog.prototype = {
                            Init:function ( ) {},
                            bindevent:function () {},
                             updateposition:function () {}                          };

2-3. The private variable name begins with an underscore. such as: "_current", "_defaultconfig"

2-4. The constant name is all uppercase and the words are separated by an underscore. such as: "Css_btn_close", "txt_loading"

2-5. Prefix of variable name:

The code is as follows Copy Code

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 should use a single-letter designation.

3. Code Formatting requirements

3-1. Necessary spaces and indents in the statement

3-1-1. Use a space before and after the "()" that contains the statement, such as: If/for/while/switch (statements) {...}

3-1-2. "=" need to be preceded by a space

3-1-3. "," after the array member needs to be followed by a space

Not good:

The code is as follows Copy Code

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

Good:

The code is as follows Copy Code

For (t in selected) {
if (!hash[t]) {
Deselect (t); }

3-2. Long statements using break line:

Not good:

Templ_songlist.replace (' {TABLE} ', da[' results ']). Replace (' {prev_num} ', PREV). Replace (' {next_num} ', NEXT). Replace ( ' {current_num} ', current '. Replace (' {total_num} ', da.page_total);

Good:

The code is as follows Copy Code
Templ_songlist.replace (' {TABLE} ', da[' results ']).
Replace (' {prev_num} ', PREV).
Replace (' {next_num} ', NEXT).
Replace (' {current_num} ', current).
Replace (' {total_num} ', da.page_total);

3-3. Format Object Parameters:

Not good:

The code is as follows Copy Code

embedSWF (ID, {URL: '/swf/player30792.swf?url= ' + el.href, width:261, height:30, params: {wmode: ' Transparent '}, attrib Utes: {ID: "player-sample" + I, Name: "Player-sample" + I}});

Good:

The code is as follows Copy Code
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.