1. Use js file management code
Try to put all the code in the js file, and then use script to introduce it in the html file. When introducing, be sure to put it after the body tag, and do not use type or language.
2. Writing indentation
Use 4 blank spaces for indentation. Be careful not to use the tab key for indentation.
3. Sentence segmentation
Pay attention to the line length. Each line should not exceed 80 characters. If it exceeds 80 characters, appropriate sentence segmentation should be performed. The sentence segmentation should be done after the operator. The ideal sentence is to be segmented after the comma (,). After the sentence segmentation, use 8 spaces for the next line. indentation.
4. Notes
Single-line comments are generally used, and block comments are generally used in documents.
5. Variable declaration
Declare all variables before use. Undeclared variables will automatically become global variables. Global variables should be used sparingly throughout the text.
It is best to use a var to declare all variables, and put each declaration on a separate line, and add comments. If possible, all declared variables are listed in character order, as follows:
6. Function declaration
All functions should be declared before use and after variables ------- to help view the scope.
There should be no spaces between the function name and the parentheses; there should be no space between the right parenthesis (and the function parameters; the left parenthesis) and the function body bracket {There should be a space between; the function body is indented by 4 spaces; the function body closing bracket} and The first character of the function declaration keyword function is aligned. The following code:
function inner(a,b) {
return (e * a) b;
}
return inner(0,1);
}
7. Naming
Name with letters, numbers, and underscores. Avoid using international characters, dollar sign $, and backslashes.
Do not use an underscore as the first character of a name.
Most variables and functions are named starting with a lowercase letter.
The constructor must start with a capital letter. Omitting new in js will not cause an error (compilation or running error), but it is best not to omit it.
Global variables should be named in all uppercase (there is no concept of macros and constants in js).
8. Statement
Simple statement
There is a maximum of one statement per line, and it ends with a semicolon;. Note that statements that assign values to function literals and object literals also use a semicolon;.
js allows any variable to be used as a statement, but it may cause some errors when inserting a semicolon. Therefore, the statements that generally use expressions are assignment or function call statements (I roughly understand the original English text of this sentence, but I don’t know how The translation is better)
Compound statement (a statement contained between a pair of {})
Internal statements are indented 4 spaces.
The opening bracket { should be at the end of the opening statement line.
The closing bracket should be on its own line at the end, aligned with the first character of the line containing the opening bracket.
When the statement is in a control statement (such as for, if, etc.), the statement should be surrounded by curly braces {}, even if there is only one statement, so as to ensure that no bugs occur when adding the statement.
9. Tag (I don’t think this part is quite right)
The statements to use label are selective, there are only the following types: 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 return keyword (avoid inserting a semicolon on a new line).
11. if statement
Follow the following format:
if (condition) {
Statements
} else {
Statements
}
if (condition) {
Statements
} else if (condition) {
Statements
} else {
Statements
}
12. for statement
Follow the following format:
for (variable in object) {
If (filter) {
statements
}
}
13. while statement
Follow the following format:
14. do-while statement
Follow the following format:
Add a semicolon at the end of the statement.
15. switch statement
Follow the following format:
Each case must be aligned with the switch to avoid excessive indentation. Only case labels are not statements and should not be indented.
Every case statement (except default) must end with break, return, or throw.
16. try statement
Follow the following format:
17. continue statement
Avoid using continue statement.
18. with statement
The with statement should not be used.
19. Use spaces
Separate logically related code segments by setting blank lines to enhance code readability.
Set spaces in the following cases:
Keywords are followed by an opening bracket (use spaces, for example:
while (true) {
You cannot use spaces between function parameters and the opening bracket (.
Binary operators except the period (.), left parenthesis ((), and square bracket ([) must use a space to separate the operands.
There should be no spaces between unary operators other than typeof and their operands.
There is a space after each semicolon; in the for statement control block ().
There must be a space after each comma.
20. Additional suggestions
[] and {}
Arrays are used when the member names are consecutive integers, objects are used when the member names are arbitrary strings and names.
Use {} instead of new object() and [] instead of new Array().
comma, operator
Avoid using commas and operators (this rule does not apply to object literals, array literal definitions, var declaration statements, and parameter lists)
Block scope
In addition to conforming to statements that do not use statement blocks, js does not have block-level scope, only function scope.
Assignment expression
Avoid using assignment statements in the conditional judgment part of while and if statements.
===and!==
Use congruent symbols (=== and !==) to determine equality and avoid using forced type equality conversion symbols (== and !=).
If a number is added (or -) to a number with a sign ( or -), or a number with a sign ( or - -), the number with a sign or ( or - -) needs to be enclosed.
eval is the devil (abuse of eval)
eval has the same situation, the Function constructor should not be used, and no string is passed to the setTimeout or setInterval function.
The above 20 suggestions are all summarized by me in the project. They should be a little helpful for novices learning javascript. They are all personal experiences. There are inevitably some imperfections. If you find them, please tell them here. Let's make progress together.