JavaScript code specifications

JavaScript files

JavaScript programs should be saved independently in files with the suffix .js.

JavaScript code should not be included in HTML files unless it is a specific section of code that belongs only to this section. JavaScript code in HTML will significantly increase the file size, and it cannot be cached or compressed.

Indentation

The indentation unit is four spaces. Avoid using the Tab key to indent (even now in the 21st century), there is still no unified standard for Tab length. Although using spaces increases the file size, it is almost negligible on a local area network and can be eliminated during the minimization process.

Length of each line

Avoid exceeding 80 characters per line. When a statement does not fit on one line, consider wrapping it. In operation symbols, it is best to put a newline after a comma. Wrapping lines after operators reduces the chance of copy-paste errors being masked by semicolons. The next line should be indented 8 spaces.

Comments

Don’t skimp on comments. It's very useful to leave information for people who will need to understand your code in the future (perhaps yourself). Comments should be as well-written and clear as the code they comment on. The occasional bit of humor is even better. Remember to avoid being wordy or emotional.

It is also important to update comments in a timely manner. Wrong comments can make a program more difficult to read and understand.

Variable declaration

All variables must be declared before use. JavaScript does not require this, but it makes the program easier to read and makes it easier to find undeclared variables (they will be compiled into global variables).

Put the var statement at the beginning of the function.

It is best to put the declaration statement of each variable on a separate line and add comments. All variables are sorted alphabetically.

var currentEntry; // Current selection

##var level; // Indentation level

var size; // Table size

Function declaration

All functions must be declared before use. The declaration of the inner function follows the var statement. This can help determine which variables are within the scope of the function.

There should be no space between the function name and ((left bracket).) (right bracket) and {(left brace) that starts the program body. A space should be inserted. Function program bodies should be indented four spaces. } (right brace) is aligned with the head of the line of code that declares the function.

function outer(c, d) {        

    var e = c * d;      

  function inner(a, b) {         

                                                                                                                                                                                               

}      

return inner(0, 1);

}

Name

The variable name should consist of 26 It consists of uppercase and lowercase letters (A..Z,a..z), 10 numbers (0..9), and _ (underscore). Avoid using internationalized characters (such as Chinese) because they are not easily read and understood everywhere. Do not use $ (dollar sign) or (backslash) in names.

Do not use _ (underscore) as the first character of the variable name. It is sometimes used to represent private variables, but in fact JavaScript does not provide the function of private variables. If private variables are important, use the private member form. This misleading naming convention should be avoided.

Most variable names and method names should begin with a lowercase letter.

Constructor names that must be used with new should start with a capital letter. JavaScript will not throw any compilation errors or runtime errors when new is omitted. If you forget to add new, bad things will happen (such as being treated as a normal function), so capitalizing the constructor name is the only way we can avoid this happening.

Global variables should be in all uppercase letters. (JavaScript does not have macros or constants, so there will be no misunderstandings)

Statement

Simple statement

Each line contains at most one statement. Put ; (semicolon) at the end of every simple statement. Note that a function assignment or object assignment statement is also an assignment statement and should end with a semicolon.

JavaScript can treat any expression as a statement. This can easily hide some errors, especially errors caused by adding a semicolon by mistake. An expression should be treated as a separate statement only when assigned and called.

Compound statement

A compound statement is a sequence of statements enclosed in { } (braces).

Enclosed statements must be indented four more spaces. { (left brace) should be at the end of the execution of a compound statement. } (right brace) should be aligned with the beginning of the line of { (left brace) Braces should be used in all compound statements, even if there is only one statement, when they are part of a control structure, such as an if or for statement . Doing this will avoid errors when adding statements later.

Marking

Statement marking is optional, only the following statements must be marked: while, do, for, switch.

return statement

Do not use ( ) (brackets) to enclose the return value in a return statement with a return value. If an expression is returned, the expression should be on the same line as the return keyword to avoid accidental semicolon errors.

continue statement

Avoid using the continue statement. It can easily make the logic of a program obscure.

with statement

Do not use the with statement.

Blank

Using blank lines to separate logically related code blocks can improve the readability of the program.

Space should be used in the following situations:

• Keywords following ((left bracket) should be separated by a space. while (true) {
• Function parameters There should be no space between ((left bracket) and ((left bracket)). This helps distinguish keywords from function calls.

• All binary operators, except .(dot) and ((left bracket) and [ (left square bracket) should be separated from the operand by a space.

• There should be no space between a unary operator and its operand, unless the operator is a word, such as typeof. • Each ; (semicolon) in a control section, such as a for statement, must be followed by a space.

• Each , (comma) should be followed by a space.

Object Rules.

Rules for object definition:

Put the opening curly bracket and the class name on the same line.

There is a space between the colon and the attribute value. Use double quotes for strings, no need for numbers.

Do not add a comma after the last attribute-value pair.

Put the right curly bracket on a separate line and end it with the symbol #.

##File extension

HTML file suffix can be .html (or r .htm).

CSS file suffix is ​​.css.

JavaScript file suffix is ​​.js.

Use lowercase file names

Most web servers (Apache, Unix) are case-sensitive: london.jpg cannot be accessed through London.jpg .

Other web servers (Microsoft, IIS) are not case sensitive: london.jpg can be accessed as London.jpg or london.jpg.

You must maintain a consistent style. We recommend using lowercase file names uniformly.


Continuing Learning
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <script type="text/javascript"> var array_1 = new Array('a','b','c',1,2); document.write( array_1.concat( "|" ) ); </script> </head> <body> </body> </html>
submitReset Code