Address: http://nodeguide.com/style.html
This is a piece of translation article on the nodejs encoding style. nodejs does not have a uniform official encoding style, but a good encoding style can improve code readability. Since some people have summarized it, you can refer to it. The original document is not fully translated, but the rules are extracted.
1. Tab and Space
Use two spaces to indicate indentation, instead of using Tab
2. semicolon
Although javascript may end the statement without a semicolon, you must add a semicolon to avoid mistakes.
3. Row Width limit
Limit the line width to 80 characters.
4. quotation marks
Unless you are writing JSON, use single quotes
5. curly brackets
Place curly braces on the same line of the statement.
6. Variable Declaration
Use var to declare variables. Add a semicolon after each variable. Do not use commas to declare variables consecutively.
7. Variable and attribute name
Use the small luofeng naming method to avoid using single-character variables and abbreviations.
8. Class Name
Use the DA luofeng naming method
9. Constants
Uppercase is used, and multiple words can be connected by underscores.
10. Create objects and Arrays
Use a comma at the end.
11. Determine equal operations
If you use the Third Equal Sign = instead of the double equal sign =, the double equal sign is automatically converted and unexpected problems occur.
12. Extension object
Do not extend the attributes of any object, especially native objects. Unexpected problems may occur if you do not comply with this rule.
13. Conditions
Any meaningful condition should have a descriptive variable, for example:
14. Function Name Length
The function name must not be too long.
15. Return Value
Avoid diving into the if statement in depth and try to obtain a return value for each function as soon as possible.
16. Closure name
Add a name for each closure.
17. Embedded Closure
Do not embed too many closures. Otherwise, it will be messy.
18. Callback Functions
Because nodejs does not block I/O operations, functions generally return their results through callback functions. Nodejs uses the first object of the callback function as the error object. You should also follow this rule when writing the callback function by yourself.
19. getters and setters
Do not use setters because it will cause more problems. getters can use it at will.