20 suggestions for programming specifications for learning JavaScript _javascript tips

Source: Internet
Author: User
Tags case statement eval first row

1, the use of JS file management code

All the code as far as possible in the JS file, and then use the script in the HTML file introduction, the introduction of the attention placed behind the body tag, and do not use type or language.

2. Writing indent

Use 4 blank spaces to indent, and be careful not to indent using the TAB key.

3. Punctuation

Note that the president, each line does not exceed 80 characters, more than the appropriate punctuation, punctuation should be followed by the operator, the most ideal is in the comma (,) after the punctuation, the next line of punctuation after the use of 8-cell indentation.

4. Annotation

Typically, a single-line annotation is used, and block annotations are generally used for documentation.

5. Variable declaration

All variables are declared before use, and undeclared variables are automatically used as global variables. The full text should use less global variables.
It is best to implement all variable declarations in one var and each declaration in a single line and add a comment stating that if all the variables declared can be listed in character order, the following are:

Copy Code code as follows:

var currententry,//Current Select table Item
Level Indent level

When all the variables are defined at the top of the function body, Var appears in the first row of the function body.

6. Function declaration

All functions should be declared before they are used and-------after a variable to help you see the scope.
The function names and parentheses should not have spaces; the closing parenthesis (there should be no space between the function arguments; the opening parenthesis) and the function body bracket {There is a space between the functions body indented 4 spaces; function body closing bracket} and function declaration keyword functions first character alignment. The following code:

Copy Code code as follows:

function outer (c,d) {
var e = c * D;

function inner (a,b) {
Return (E * a) + B;
}

return inner (0,1);
}


Functions and objects can be placed anywhere that an expression is allowed.
anonymous function key function and left parenthesis (there is a space between.)
Use global functions as little as possible.
For an immediate execution function, the entire call expression should be placed inside a pair of parentheses () to clarify that the value of the variable is the result of the function execution rather than the function itself. The following code:
Copy Code code as follows:

var result = (function () {
var key = "";
return {
Get:function () {
Return key;
},
Set:function (key) {
key = key;
}
};
}());

7, naming

Names with letters, numbers, and underscores to avoid the use of international characters, dollar sign $, backslash \.
Do not use underscores as the first character of the name.
Most variables and functions are named with lowercase letters.
Constructors must begin with uppercase letters, omitting new in JS does not error (compile or run errors), but it is best not to omit them.
Global variables apply all uppercase names (there is no concept of macros and constants in JS).

8, statements

Simple statement

One statement at the top of each line, with a semicolon, ending with a semicolon for statements that assign values to function literals and object literals;.
JS allows any one variable as a statement, but when inserting a semicolon may cause some errors, so the general use of the expression of the statement is assignment or function call statement (this English text I probably understand, but do not know how to translate it better)

Compound statement (a statement contained between a pair of {})

The internal statement indents 4 spaces.

The opening parenthesis {should be at the end of the start statement line.
The closing parenthesis should be on the last single line and aligned with the first character of the line where the opening parenthesis is located.
When statements are in a control statement (for example, for, if, and so on), you should surround the statement with curly braces {}, even if there is only one statement, which guarantees that no bugs will be generated when you add the statement.

9, the label (this part of the understanding does not feel very right)

The statements to use the label are selective, with only the following: while, for, do, switch.

10. Return statement

The returned value should be enclosed in parentheses, and the return expression should be on the same line as the Returns keyword (avoid inserting a semicolon with a newline).

11, if statement

Follow the following format:

Copy Code code as follows:

if (condition) {
Statements
}

if (condition) {
Statements
} else {
Statements
}

if (condition) {
Statements
else if (condition) {
Statements
} else {
Statements
}

12, for statement

Follow the following format:

Copy Code code as follows:

for (initiliazation; condition update) {
Statements
}

For (variable in object) {
if (filter) {
Statements
}
}


The first type of looping format is used for arrays and variables that can determine the number of iterations.
Second, for object traversal
Note: It is mentioned here that the attributes added in the object prototype can be enumerated, so the hasOwnProperty method is used for filtering, but when I test with the for in code, there is no indication of where the problem is.

13, while statement

Follow the following format:

Copy Code code as follows:

while (condition) {
Statements
}

14, Do-while statement

Follow the following format:

Copy Code code as follows:

do {
Statements
while (condition);

To add a semicolon to the end of the statement.

15. Switch statement

Follow the following format:

Copy Code code as follows:

switch (expression) {
Case expression:
Statements
Default
Statements
}

Each case should be aligned with the switch, avoid excessive indentation, only case labels are not statements, should not be indented.
Each case statement (except default) must end with a break or return or throw.

16, try Statement

Follow the following format:

Copy Code code as follows:

try {
Statements
} catch (variable) {
Statements
}
try {
Statements
} catch (variable) {
Statements
finally {
Statements
}

17, continue statement

Avoid using continue statements.

18, with statement

The WITH statement should not be used.

19, Space use

You can increase the readability of your code by setting up blank lines to split logically related snippets.
Set a space in the following situations:
After the keyword follows the opening parenthesis (to use a space, for example:
while (true) {
You cannot use spaces between function arguments and opening parentheses.
The two-dollar operator except the dot (.), Zoo bracket ((), bracket ([)) is split by using an empty cells and operand.
There should be no space between the unary operator other than TypeOf and his operand.
The For statement controls each semicolon in the block (), followed by a space.
There should be a space after each comma.

20. Additional Suggestions

[] and {}
An array is used when the member name is a contiguous integer, and the object is used when the member name is any string and name.
Use {} Instead of new object () to replace the new Array () with [].
Comma, operator
Avoid using commas, which do not apply to object literals, array literal definitions, VAR declaration statements, and parameter lists.
Block-level scopes
In addition to conforming statements that do not use statement blocks, JS does not have block-level scopes, only function scopes.
An assignment expression
The Conditional Judgment section in the while and if statements avoids the use of assignment statements.
= = = and!==
Decision equality uses the congruent symbols (= = = and!==) to avoid the use of coercion type equality conversion symbols (= = and!=).
If a number plus (or-) a number with a symbol (+ or-), or a number with (+ + or-), you need to enclose numbers with symbols or (+ + or-).
Eval is the Devil (eval's abuse L)
Eval has the same situation, the function constructor should not be used, and no strings are passed to the settimeout or setinterval functions.

The above 20 suggestions are summed up in my project, for beginners to learn JavaScript should be a small help, are personal experience, it is inevitable that there is not a comprehensive place, if found, please tell, here, we make progress together.

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.