Most complete JavaScript coding specification (recommended) _javascript tips

Source: Internet
Author: User

1. Embedding rules

JavaScript programs should be included in the. js file as much as possible in the form of <script src= "Filename.js" > in the page when called. JavaScript code is not specific to the page, you should try to avoid writing JavaScript code directly in the page.

2. Align indent and line feed

A) indentation

In the same system should adopt the same indentation standard, this article advocates the indentation size is 4 spaces. Each compiler has a different definition of the white space size that the TAB key replaces. It is recommended that you reset the tab shortcuts in the editor to 4 spaces when you set up your development environment. Most compilers provide this functionality. Otherwise, it is recommended to indent by 4 spaces.

b) Line Change

The line must be wrapped at the following location:

After the end of each independent statement;

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

Operator must start at the beginning of a new line when the row is changed.

The policy in the reference line length is delimited for line wraps that result from the line length exceeding the limit.

1). Long string truncation

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

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

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

2). The ternary operator is too long

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

The following conditions must not occur:

3. Too long logical combination of conditions

When 80 characters cannot satisfy a requirement because of a more complex combination of logical conditions, each condition should be separated by a single row, a logical operator is placed at the beginning of the line, or part of the logic is separated by a logical combination. Finally, the closing parenthesis is placed on a separate line with the left brace {to ensure that the statement block in the if can be easily visually identified. Such as:

4. Too long JSON and array

If there are more object properties that cause each property row to take up too much space, an organization that can group chapeau or logically, such as:

Each row is controlled within a reasonable range by a grouping of 5 groups, and is split logically. You can also use the same method for arrays with more items, such as:

5). Return statement

Return if you use the execution of an expression as the returned value, put the expression and return in the same row so that the line break is incorrectly resolved to the end of the statement and cause an error to be returned. Returns undefined if no expression is returned after the return keyword. The default return value for the constructor is this.

Example:

3. Naming

Named methods usually have the following categories:

a). Description of nomenclature

1). Camel nomenclature, shaped like thisisanapple

2. Pascal nomenclature, shaped like thisisanapple

3). Underline nomenclature, shaped like this_is_an_apple

4). Underlined nomenclature, shaped like this-is-an-apple

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

B. Variable name: You must use the Camel naming method

c). Parameter name: You must use the Camel method of naming

d). Function Name: You must use the Camel naming method

E). Method/attribute: You must use the Camel naming method

f). Private (Protection) member: must begin with an underscore _

g). Constant Name: You must use all uppercase underline naming methods, such as is_debug_enabled

h). Class Name: Pascal nomenclature must be used

i). Enumeration name: Pascal nomenclature must be used

j). Attributes of an enumeration: You must use the underscore naming method for all caps

k). Namespaces: Must use the Camel naming method

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

Variable names should use nouns;

Boolean types should use is, has, and so on to indicate their type;

The function name should use the verb-object phrase;

Class names should be in nouns.

4. Note

Note to be as simple and clear as possible. To annotate a less intuitive part:

(Of course this is not recommended as a way to define a bunch of global variables directly)

In addition, the JavaScript annotation has two kinds of "//" and "* .... * *", recommended "//" as line of code comments, "/* ... *" form for the entire code section of the cancellation, or more formal declarations, such as function parameters, functions, file functions, etc. in the description:

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

5. Statement

1). Declaration of variables

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

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

There is a difference between the variable Valuea declared with Var and the VALUEB variable. It is particularly noteworthy that variables declared within functions with VAR are local variables, which effectively avoid errors caused by the same name as local variables and global variables.

2). Declaration of functions

The function should also be declared before the call, and the intrinsic function should be declared after the statement that the Var declares the internal variable, which clearly indicates the scope of the internal and internal functions.

In addition, the function name has a space between the opening parenthesis ' (between the ' and the right parenthesis ') and the ' {' behind ' {' to clearly show the function name with its parameter part, and the start of the function body. If the function is an anonymous/nameless function, then the function keyword and the left parenthesis ' (' between ') are blank, or you may mistakenly assume that the function is called a functions.

An example of an internal function sound:

The output from the example above shows that the InF () function takes effect only within the OUTF () function, and the local variable Innera the scope of the internal function. This encoding allows the scope of variables and functions to become clearer.

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.