This is a set of procedures applicable to JavaScript coding norms. It is based on Sun's Java program code norms. However, a drastic changes, because JavaScript is not Java.

Long-term value of the software directly from the quality of its encoding. In its entire life cycle, a process likely to be many people to read or modify. If a program can show a clear structure and its characteristics, it can be reduced in the future modify the possibility of error.

Programming specification will help programmers to increase the robustness of the procedure.

All the JavaScript code are exposed to the public. Therefore, we should guarantee its quality.

Very important to maintain cleanliness.

JavaScript file
JavaScript program stored in the suffix should be called independent. Js file.

JavaScript code should not be included in the HTML document, unless this is only a particular segment of code in this section. The JavaScript in the HTML code will be a marked increase in file size, but also can not be cached and compressed.

filename.js> should be on the back of body. This script can be reduced as a result of loading the contents of other pages have been included in the issue of delay. There is no need for the use of language or type attributes. MIME type is not from the server to determine the scripttag.

Indent
Indented four spaces for the units. Avoid using the Tab key to indent (even now in the 21st century), and have never been a unified standard Tab length. Although the use of space will increase the file size, but in the local area network is almost negligible, and in the process of minimization can also be eliminated.

The length of each line
Each line to avoid more than 80 characters. When his party was no less than a statement, please consider the folding line. Symbols in computing, it is best wrapped after the comma. In line after the operator can be reduced because of errors copy and paste to cover up the chance to be a semicolon. The next line should be indented 8 spaces.

Notes
Notes not to be grudging. To after the code you need to understand people (perhaps yourself) leave a message is very useful. Notes Notes should be and they are the same as the code is well written and clear. Occasionally, even a small good humor. Remember to avoid lengthy or emotional.

Update Notes in a timely manner is also very important. Note the wrong procedure will be more difficult to read and understand.

To allow meaningful comments. Focus in the interpretation of those who do not easily understand the logic immediately. Do not waste time on the reader to read similar to:

i = 0; / / so that i is equal to 0 to use single notes. Notes for the Notes block official documents and useless code.

Variable declaration
All the variables must be used prior to the statement. JavaScript is not mandatory to do so, but the procedure can do this easy-to-read, and also those who are not easily found in the variable declaration (which will be compiled into a global variable).

Var statement will be placed on the first function.

The best statement to each variable a separate statement on the line, and add notes. All the variables in alphabetical order.

var currentEntry; / / currently selected item var level; / / indent the extent of var size; / / table does not block the size of JavaScript, which is why the definition of variables in the block which will likely give rise to C / C + + / Java programmers misunderstanding. In the first definition of function of all variables.

To minimize the use of global variables. Do not let local variables global variables covered.

Function statement
Function in the use of all of the statements before. Statement within the function with the var statement in the back. This can help determine which variables are within the scope of the function.

Function names with the ((left parenthesis) There should be no space between.) (Right bracket) and the beginning of the procedure body of the ((left brace) should be inserted in a space between. Function procedure body should be indented four spaces. ) (Right brace) and the statement of functions that the head alignment lines of code.

function outer (c, d) (var e = c * d; function inner (a, b) (return (e * a) + b;) return inner (0, 1);) The following can be written in such a JavaScript in normal use, because in JavaScript, the function and object declarations can be put in place to allow any expression. And it allows inline function and the hybrid structure has the best readability.

function getElementsByClassName (className)
(
var results = [];
walkTheDOM (document.body, function (node)
(
var a; / / class name array
var c = node.className; / / node class name
var i; / / loop counter / / If the node has a class name, then split it into a list of simple names. / / If any of them match the requested name, then append the node to the set of results.
if (c) (
a = c.split ( '');
for (i = 0; i <a.length; i + = 1)
(
if (a [i] === className)
(
results.push (node);
break;

)

)
)
));
return results;
) If the function is an anonymous function, then function and ((left parenthesis) should be a space between. If omitted the spaces, otherwise they will feel for the function named function.

div.onclick = function (e)
(
return false;
);
that =
(
method: function ()
(
return this.datum;
),
datum: 0
); Try not to use the global function.

Named
26 variable names should be upper and lower case letters (A.. Z, a.. Z), 10 digits (0 .. 9), and _ (underscore) component. Avoid the use of international characters (eg Chinese), because they are not in any place can be easy to read and understand. Do not use the name $ (dollar sign) or (backslash).

Not to _ (underscore) as the variable name of the first character. It is sometimes used to indicate private variables, but JavaScript does not provide the function of private variables. Very important if the private variable, then use the form of private members. Should avoid the use of such names easily misunderstood habits.

Most of the variable names and methods of life should be at the beginning of lowercase letters.

Must be shared with the new name of the constructor should be at the beginning of capital letters. When new was omitted when the JavaScript will not have any compiler errors or run out wrong. Add new time will forget the bad things happen (such as to be regarded as a function of the general), the capital structure to function names that we try to avoid such a scenario the only way.

Global variables should be all caps. (JavaScript is not constant or macro, it will not cause misunderstanding)

Statement
Simple statements
Each line contains only a statement. To; (semicolon) on each end of a simple statement. Attention to a function or object assignment assignment assignment is, it should be a semicolon at the end.

JavaScript can be used as a statement of any expression. It's easy to hide certain errors, especially errors mistakenly increase semicolon. And call only if the assignment, the expression should be treated as a separate statement.

Compound statement
Compound statement is contained in () (braces) sequence of statements.

Including the effect of the statement was to be more than four space indent.
((Left brace) should be in the compound statement at the end of its implementation.
) (Right brace) should be consistent with ((left brace) at the beginning of the line alignment
Braces all the compound statement should be used, even if only one statement, when they are part of control structure, such as an if or for statement. So doing, we can add a statement after the error caused.
Marked
Labeled statement is optional, and only the following statement must be labeled: while, do, for, switch.

return statement
To return to the value of a statement not to use the return () (in brackets) to enclose the return value. If the return expressions, regular expressions with the return keyword in the same line, in order to avoid misuse of the semicolon error increases.

if statement
if statement should be as in the following format:

if (condition) (
statements;
)

if (condition) (
statements;
) Else (
statements;
)

if (condition) (
statements;
) Else if (condition) (
statements;
) Else (
statements;
)

for statement
for statements should be as in the following format:

for (initialization; condition; update) (
statements;
)

for (variable in object) if (filter) (
statements;
)

The first cycle of the form of already know the relevant parameters for the array cycle.

The second form applies to object. The members of the prototype object will be included in the iterator in. HasOwnProperty through pre-defined way to distinguish between members of the real object of a good method:

for (variablein object) if (object.hasOwnProperty (variable)) (
statements;
)

while statement
while statement should be as in the following format:

while (condition) (
statements;
)

do statement
do statement should be similar to the following format:

do (
statements;
) While (condition);

Unlike other compound statements, do statements always; (semicolon) at the end.

switch statement
switch statement should be as in the following format:

switch (expression) (
case expression:
statements;
default:
statements;
)

Alignment with the switch in each case. This will avoid over-indent.

Each group of statements (in addition to default should be to break, return, or throw at the end. Do not let it down sequential implementation.

try statement
try statement should be similar to the following format:

try (
statements;
) Catch (variable) (
statements;
)

try (
statements;
) Catch (variable) (
statements;
) Finally (
statements;
)

continue statement
Avoid using the continue statement. It is very easy to program the logic of the process of making gimmicks.

with statement
Do not use the with statement.

Blank
With blank lines to the logic of the relevant block of code can be separated to improve the readability of the procedure.

Spaces should be used in the following circumstances:

With the ((left parenthesis) after the keyword should be separated by a space.
while (true) (function parameters and the ((left parenthesis) There should be no spaces between. This can help to distinguish between keywords and function calls.
All the binary operators, in addition to. (Points) and ((left parenthesis) and [(left bracket) applications space will be separated from their operands.
Operator one dollar and its operation should not have spaces between, unless the operator is a word, such as typeof.
In the control of each part, such as for the statement; (semicolon) with a space after.
Each, (comma) with a space after.
Alternative proposals
() And []
Use () instead of new Object (). Use [] instead of new Array ().

When the members are a group of figures in an orderly manner to preserve the use of array data. When the members are not the law of the use of string or other object to save data.

, (Comma) operator
Avoid the use of a comma operator, except for words in specific parts of the control. (This does not include those objects used in the definition, the definition of the array, var statement, and parameters in the list of comma separator.)

Scope
JavaScript is not in the domain block. There are only a function of the domain. Do not use the blocks, except in the compound statement.

Assignment expression
Avoid if statements and while part of the conditions of assignment.

if (a = b) (is a correct statement? or

if (a == b) (is right? avoid this is not easy to determine the structure of right and wrong.

=== And! == Operator.
Use === and! == Operator will be relatively better. == And! = The type of operator will be cast. In particular, should not be used with the wrong == value (false, null, undefined, "", 0, NaN).

Confusing the plus and minus
Care immediately after the + + or + +. This form is still very easy to confuse people. Brackets should be inserted in order to facilitate understanding.

total = subtotal + + myInput.value; best to put down

total = subtotal + (+ myInput.value); such a + + will not be mistaken for the + +.

eval is the devil
eval is the most JavaScript method can easily be abused. Avoid the use of it.

eval has aliases. Do not use the Function constructor. SetTimeout or setInterval not to pass a string parameter.
Original Address: http://javascript.crockford.com/code.html