JavaScript Coding Specification

Source: Internet
Author: User

1. Embedding Rules

JavaScript program should be placed in the. js file, need to call the time in the page in the form of <script src="Filename.js" > included in. if the javascript Code is not dedicated to the page, you should try to avoid writing JavaScript code directly in the page .

2. aligning indents and line breaks

A) indent

The same indentation criteria should be used in the same system, and this article advocates a indentation size of 4 spaces. Each compiler has a different definition of the whitespace size that the TAB key replaces. It is recommended that you set the tab shortcut key in the editor to 4 spaces when setting up the development environment. Most compilers provide this functionality. Otherwise it is recommended to indent by 4 spaces.

b) line break

The line must be wrapped in the following location:

After the end of each independent statement;

If,else,catch,finally, while and other keywords before;

operator, the operator must be at the beginning of the new line at the start of a newline.

For line breaks that result from a single line length exceeding the limit, the policy in the reference line length is delimited.

1). String too long truncation

Each line of code should be less than 80 characters. If the code is longer should be wrapped as far as possible, the newline should be selected after the operator and punctuation, preferably in the semicolon ";" or comma "," after. The next line of code indents 4 spaces in relation to the previous line. This effectively prevents errors such as code deletions caused by copy-and-paste and enhances readability.

Truncates the string by a certain length and connects using the + operator. The delimited string is as chapeau as possible, such as not to break in the middle of a complete noun. In particular, for the stitching of HTML fragments, the same structure is maintained with HTML by indenting:

You can also use arrays for stitching, which makes it easier to adjust indents relative to the + operation:

2). The ternary operator is too long

The ternary operator is made up of 3 parts, so its wrapping should be different depending on the length of each part, forming 3 different situations:

The following conditions must not occur:

3). A combination of long logical conditions

When 80 characters fail to meet the requirements because of a complex combination of logical conditions, each condition should be separated by a single line, a logical operator placed at the beginning of a row, or a logical combination of some logic. Finally, the closing parenthesis) and the opening brace {on a separate line, ensure that the statement block with the if inside can be easily visually identifiable. Such as:

4). JSON and arrays that are too long

If you have more object properties that cause each property to occupy too much space, you can chapeau or logically group the Organization, such as:

By grouping 5 groups, each row is controlled within a reasonable range and logically sliced. For arrays with more items, you can also use the same method, such as:

5). Return statement

Return if you use the execution of an expression as the return value, put the expression and return on the same line so that the newline character is mistakenly resolved to the end of the statement, causing an error to be returned. Returns the undefined if no expression is returned after the return keyword. The default return value for the constructor is this.

Example:

3. naming

Named methods typically have the following categories:

A). Description of the naming Act

1). Camel nomenclature, shaped like thisisanapple

2). Pascal nomenclature, shaped like thisisanapple

3). Underline naming method, shaped like This_is_an_apple

4). The name of the this-is-an-apple in the form of

According to different types of content, the following nomenclature must be strictly used:

b). Variable name : You must use the Camel name method

c). Parameter name : Camel name must be used

d). Name of the function : The camel name method must be used

e). Method/attribute : Camel name must be used

f). Private (Protected) member : must begin with the following dash _

g). Constant name : You must use the all-uppercase underline naming method, such as is_debug_enabled

h). Class name : Pascal nomenclature must be used

i). Enumeration name : Pascal nomenclature must be used

j). Properties of the enumeration : You must use the Underline naming method in all capitals

k). Namespaces : You must use the Camel naming method

l). Semantics : Naming also requires attention to semantics, such as:

Variable names should use nouns;

The Boolean type should use the IS, have, and so on to indicate its type;

The function name should use the verb-object phrase;

The class name should be used as a noun.

4. Notes

Note to be as simple as possible, clear and concise. Focus on the meaning of the note, the less intuitive part of the annotation:

(This is not recommended for direct definition of a bunch of global variables)

In addition, the comments forJavaScript have two "//" and "/* .... * *", suggested "// " As a line of code comment,the "/* .... *" form is used as a logout of the entire code snippet, or in a more formal declaration, such as a function parameter, function, file function, and so on:

Another: Copy and paste should note whether the comment corresponds to the code.

5. Disclaimer

1). Declaration of variables

Although the JavaScript language does not require variables to be declared before they are used. But we should develop this good habit. This makes it easier to detect undeclared variables and avoid them becoming hidden global variables, causing pitfalls.

At the beginning of the function, the var keyword is used to declare the local variables to be used in the function, the function of the comment variable and the meaning of the representation, and should be sorted alphabetically. Each variable occupies a single row to add a comment. This is because only the {} of the function in JavaScript indicates scope, and Local variables declared with the VAR keyword are valid only within the function, without variables declared by VAR are considered global variables. Example:

There is a difference between the variable Valuea declared with Var and the variable Valueb that is not declared . In particular, it is important to note that variables declared with var inside a function are local variables, which effectively avoids errors caused by local variables and global variables with the same name.

2). declaration of a function

The function should also be declared before the call, and the inner function should be declared after the statement of the var declaration internal variable, which clearly indicates the scope of the inner variable and the inner function.  

") ' and later ' {' There is a space between them to clearly show the function name with its argument part, and the beginning of the function body. If the function is anonymous &NBSP; / Span style= "Font-family:verdana;" >&NBSP, &NBSP; Span style= "FONT-FAMILY:CALIBRI;" >function keyword and opening parenthesis "(' Span style= "Font-family:verdana;" >  .

Examples of internal function sounds:

As can be seen from the output of the above example, theInF () function only takes effect inside the Outf () function, and the local variable Innera takes effect on the scope of the intrinsic function. This way of coding makes the scope of variables and functions clear.

15.12.21 various extracts

JavaScript Coding Specification

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.