1. Use js file management code
All code should be placed in the js file as much as possible, and then imported in the html file using script. When introducing it, please pay attention to the body tag and do not use type or language.
2. Writing indents
Use 4 blank spaces to indent, be careful not to use the tab key to indent.
3. Sentence breaking
Pay attention to the length of the line. Each line does not exceed 80 characters. If it exceeds it, make appropriate sentence breaking. The sentence breaking should be performed after the operator. The ideal is to make a sentence breaking after the comma (,). After the sentence breaking, the next line is indented using 8 grids.
4. Notes
Single-line comments are generally used, and block comments are generally used in documents.
5. Variable declaration
All variables are declared before use, and undeclared variables are automatically used as global variables. Global variables should be used less in the whole text.
It is best to implement all variable declarations in one var, and each declaration is put a separate line and add comment instructions. If all declared variables can be listed in character order, as follows:
The code copy is as follows:
var currentEntry, //Currently selected table item
level; //Indent level
Define all variables at the top of the function body, and var appears in the first line of the function body.
6. Function declaration
All functions should be declared before use and after variables --------------------------------------------------------------------------------------------------------------------
There should be no spaces in the function name and parentheses; there should be no spaces between the closing bracket (there should be no spaces between the function parameters; the left bracket) and the function body bracket {; the function body is indented 4 spaces; the function body ends bracket } is aligned with the function declaration keyword function first character. The following code:
The code copy 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 anywhere that allows expressions to be placed.
Anonymous function keyword function and the opening bracket (there is a space between it.
Try to use global functions as little as possible.
For immediate execution of functions, the entire calling expression should be placed in a pair of brackets () to clarify that the value of the variable is the result of the function execution rather than the function itself. The following code:
The code copy is as follows:
var result = (function () {
var key = "";
return {
get: function () {
return key;
},
set: function (key) {
key = key;
}
};
}());
7. Naming
Name it with letters, numbers, and underscores, avoid using international characters, dollar signs, $, and backslashes/.
Do not use underscores as the first character of the name.
Most variables and functions are named starting with lowercase letters.
The constructor must start with capital letters. If you omit new in js, you will not report an error (compile or run error), but it is best not to omit it.
Global variables are named in all capitals (there is no concept of macros and constants in js).
8. Sentence
Simple statement
At most one statement per line, and use a semicolon; at the end, note that statements assigned with function literals and object literals should also use a semicolon;.
js allows any variable as a statement, but it may cause some errors when inserting a semicolon. Therefore, the general statements that use expressions are assignment or function call statements (I roughly understand this original English sentence, but I don’t know how to translate it better)
Compound statement (statements contained between a pair of {})
The internal statement is indented by 4 spaces.
The opening bracket { should be at the end of the start statement line.
The closing bracket should be a separate line at the end and align with the first character of the line where the opening bracket is located.
When a 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, which ensures that there will be no bugs when adding a statement.
9. Label (The understanding of this part is not very correct)
Statements to use label are selective, there are 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 return keyword (avoid newline insertion of semicolons).
11. If statement
Follow the following format:
The code copy is 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:
The code copy 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.
The second is used for object traversal
Note: It is mentioned here that the properties added in the object prototype are enumerable, so we need to use the hasOwnProperty method for filtering, but when I tested it with the for in code, it was not displayed. I don’t know where the problem lies.
13. While statement
Follow the following format:
The code copy is as follows:
while (condition) {
statements
}
14. do-while statement
Follow the following format:
The code copy is as follows:
do {
statements
} while (condition);
Add a semicolon at the end of the statement.
15. Switch statement
Follow the following format:
The code copy is as follows:
switch (expression) {
case expression:
statements
default:
statements
}
Each case must be aligned with the switch to avoid excessive indentation. Only the case tag is not a statement and should not be indented.
Each case statement (except default) must end with break or return or throw.
16. Try statement
Follow the following format:
The code copy is 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. Use spaces
Set empty lines to split logically related code segments to enhance code readability.
Set spaces when:
The keyword is followed by the left bracket (to use spaces, for example:
while (true) {
You cannot use spaces between function parameters and the opening brackets (.
Binary operators except dot (.), left bracket ((), and square bracket ([) must be separated from the operand using a space.
There should be no space between the unary operator except typeof and its operand.
The for statement controls each semicolon in block(); followed by a space.
Each comma must have a space after it.
20. Additional suggestions
[]and{}
Use arrays when the member name is a continuous integer, and objects when the member name is an arbitrary string and name.
Use {} instead of new object() and [] instead of new Array().
Comma, operator
Avoid using commas, operators (this provision does not apply to object literals, array literal definitions, var declaration statements, and parameter lists)
Block-level scope
Except for the conforming statements that do not use statement blocks, js has no block-level scope, only function scope.
Assignment expression
The conditional judgment part in the while and if statements avoids the use of assignment statements.
=== and!==
Use congruent symbols (=== and !==) to determine equality, and avoid using forced type equality conversion symbols (== and !=).
If a number is added (or -) a number with a symbol (+ or -) or a number with (++ or --), then a number with a symbol or (++ or --) needs to be enclosed.
eval is a demon (eval abuse l)
There is the same situation in eval, the Function constructor should not be used, and strings should not be passed to the setTimeout or setInterval function.
The above 20 suggestions are summarized by me in the project. They should be of little help to beginners in learning JavaScript. They are all personal experiences. There are inevitably incomplete aspects. If you find them, please tell me. Here we will lead to the progress of everyone.