20 tips for learning javascript programming specifications _ javascript skills

Source: Internet
Author: User
This article summarizes 20 javascript programming specifications, all of which need to be paid attention to when used in projects. We recommend them to you here. If they are helpful to you, it is excellent. 1. Use js File Management Code

Put all the code in the js file as much as possible, and then use the script to introduce it in the html file. Pay attention to putting the code behind the body tag and do not use the type or language.

2. Writing indent

Use four blank spaces for indentation. Do not use the tab key for indentation.

3. Sentence breaking

Note that the length of each line should not exceed 80 characters. When the length is exceeded, a proper sentence should be broken, and the sentence should be followed by the operator. Ideally, a sentence should be broken after a comma, the next line behind the sentence is indented with 8 cells.

4. Annotations

A single line comment is generally used, and a block comment is generally used for documents.

5. Variable Declaration

All variables are declared before they are used. Undeclared variables are automatically used as global variables. The full text should use less global variables.
We recommend that you use one var for all variable declarations, put a single row for each declaration, and add comments. If you can list all declared variables in character order, as shown below:

The Code is as follows:


Var currentEntry, // The current selected table Project
Level; // indent level


If all variables are defined at the top of the function body, var appears in the first line of the function body.

6. function declaration

All functions should be declared before they are used, and ------- is performed after the variable to help you view the scope.
There should be no space between the function name and the brackets. There should be no space between the right brackets (and function parameters; left brackets) and the function braces; four spaces are indented to the function body. The ending brackets of the function body are aligned with the first character of the function. The following code:

The Code is 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 in any place where expressions are allowed.
There is a space between the function keyword and the left brace of the anonymous function.
Use as few global functions as possible.
For immediate function execution, the entire call expression should be placed in a pair of parentheses () to clarify that the variable value is the function execution result rather than the function itself. The following code:

The Code is as follows:


Var result = (function (){
Var key = "";
Return {
Get: function (){
Return key;
},
Set: function (key ){
Key = key;
}
};
}());

7. Name

It is named with letters, numbers, and underscores. Do not use international characters, dollar signs $, backslash \.
Do not use underscores (_) as the first character of the name.
Most variables and functions start with a lowercase letter.
The constructor must start with an upper-case letter. If new is omitted in js, no error (compilation or running error) is returned, but it is best not to omit it.
The global variables are all named in uppercase (the macro and constant concepts are not found in js ).

8. Statements

Simple statement

Each line can contain up to one statement and end with a semicolon (;). Note that you must use a semicolon (;) to assign values to function and object literal values ;.
Js allows any variable as a statement, but some errors may occur when a semicolon is inserted, therefore, the statements using expressions are usually value assignment or function call statements (I have understood the original English sentence, but I do not know how to translate it better)

Composite Statement (contains a pair of {} statements)

The internal statement is indented with four spaces.

The left parenthesis {should be at the end of the Start statement line.
The right brace should be in the last separate line and aligned with the first character of the line in which the left brace is located.
When a statement is in a control statement (for example, for, if, etc.), enclose the statement with curly braces {}, even if there is only one statement, this ensures that no bugs are generated when you add statements.

9. Labels(This part does not feel quite correct)

The label statement is optional. There are only the following types: while, for, do, and switch.

10. Return Statement

The returned value should be enclosed in parentheses, And the return expression should be in the same line as the return keyword (avoid inserting a semicolon in a line break ).

11. if statement

Follow the following format:

The Code is as follows:


If (condition ){
Statements
}

If (condition ){
Statements
} Else {
Statements
}

If (condition ){
Statements
} Else if (condition ){
Statements
} Else {
Statements
}

12. for statements

Follow the following format:

The Code is as follows:


For (initiliazation; condition; update ){
Statements
}

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


The first loop format is used for arrays and variables that can determine the number of iterations.
2 For object Traversal
Note: The attributes added in the object prototype can be enumerated. Therefore, you must use the hasOwnProperty Method for filtering. However, the attributes are not displayed when I test the for in code, I don't know where the problem is.

13. while statement

Follow the following format:

The Code is as follows:


While (condition ){
Statements
}

14. do-while statement

Follow the following format:

The Code is as follows:


Do {
Statements
} While (condition );

Add a semicolon to the end of the statement.

15. switch statement

Follow the following format:

The Code is as follows:


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

Each case must be aligned with the switch to avoid over-indent. Only case labels are not statements and should not be indented.
Each case statement (except default) must end with a break, return, or throw.

16. try statement

Follow the following format:

The Code is as follows:


Try {
Statements
} Catch (variable ){
Statements
}
Try {
Statements
} Catch (variable ){
Statements
} Finally {
Statements
}

17. continue statement

Avoid using the continue statement.

18. with statement

The with statement should not be used.

19. Use space

You can use empty lines to separate the code segments related to logic to improve code readability.
Set spaces in the following cases:
The keyword is followed by left parentheses (use spaces, for example:
While (true ){
You cannot use spaces between function parameters and left brackets.
Binary operators except the dot (.), left parentheses (), and square brackets ([) must use a space to separate them from the operands.
There should be no space between the unary operator except typeof and Its operands.
Each semicolon in the for statement control block (), followed by a space.
Each comma must be followed by a space.

20. Additional suggestions

[] And {}
An array is used when the member name is a continuous integer, and an object is used when the member name is any string or name.
Use {} instead of new object (), and use [] instead of new Array ().
Comma, Operator
Avoid using commas and operators (this rule does not apply to object literal volume, array literal volume definition, var declaration statement, and parameter list)
Block-level scope
Except that the statement does not use statement blocks, js does not have block-level scope and only function scope.
Value assignment expression
In the while and if statements, the condition judgment part should not use the value assignment statement.
===And! =
Use the equal sign (=) and! =), Avoid using the forced type equal conversion symbols (= and! = ).
If a number plus (or-) a number with a symbol (+ or-) or a number with (+ + or, you need to enclose a number with a symbol or (++ or.
Eval is a demon (eval abuse l)
If the eval is the same, the Function constructor should not be used and a string is not transmitted to the setTimeout or setInterval functions.

The above 20 suggestions are summarized by myself in the project. It is helpful for beginners to learn javascript. They all have personal experience and are not comprehensive, let us also tell you, here we will give you a reference and make common progress.

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.