Home > Web Front-end > JS Tutorial > 20 Tips for Learning JavaScript Programming Standards_Javascript Tips

20 Tips for Learning JavaScript Programming Standards_Javascript Tips

WBOY
Release: 2016-05-16 16:29:53
Original
1455 people have browsed it

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:

Copy code The code is as follows:

var currentEntry, //Current selection table item
Level; //Indentation level

Define all variables at the top of the function body, then var appears on the first line of the function body.

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:

Copy 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 anywhere expressions are allowed.
There is a space between the anonymous function keyword function and the left bracket (.
Use global functions as little as possible.
For immediate execution of functions, the entire calling expression should be placed within a pair of parentheses () to make it clear that the value of the variable is the result of the function execution rather than the function itself. The following code:
Copy code The code is as follows:

var result = (function () {
var key = "";
Return {
          get: function () {
              return key;
},
set: function (key) {
              key = key;
}
};
}());

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:

Copy code The code 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:

Copy code 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.
The second one is used for object traversal
Note: It is mentioned here that the properties added in the object prototype can be enumerated, so the hasOwnProperty method must be used to filter. However, 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:

Copy code The code is as follows:

while (condition) {
Statements
}

14. do-while statement

Follow the following format:

Copy code The code is as follows:

do {
Statements
} while (condition);

Add a semicolon at the end of the statement.

15. switch statement

Follow the following format:

Copy code The code is as follows:

switch (expression) {
case expression:
Statements
default:
Statements
}

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:

Copy code The code is as follows:

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

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template