JavaScript programming style

Source: Internet
Author: User

1. Brackets
Rule 1: braces at the beginning of the block. Do not start another line.
2. parentheses
In JavaScript, parentheses have two functions: one for calling a function and the other for a combination of different values. We can use spaces to distinguish the two different brackets.
Rule 2: When a function is called, there is no space between the function name and the left bracket.
Rule 3: there is no space between the function name and the parameter Sequence
Rule 4: there is a space between all other syntax elements and the left parenthesis.
3. semicolon
In most cases, if you omit the semicolon, JavaScript will automatically add
Rule 5: Do not skip the last semicolon
4. With statement
With statement can be reduced Code But may cause confusion.
Rule 6: Do not use the with statement
5. Equal and strictly equal
The equality operator automatically converts the variable type, resulting in many unexpected situations.
Rule 7: Do not use "equal" (=), only use the "strict equal" (=) Operator
6. Statement Merging
Rule 8: Do not merge statements for different purposes into one row.
7. Variable Declaration
Javascript automatically promotes the variable life to the header of the code block.
Rule 9: all variables are stored in the function header.
Rule 10: All functions are defined before use.
8. Global Variables
Global variables are readable and writable for any code block. This is not applicable to code modularization and reuse.
Rule 11: Avoid using global variables. If you have to use them, use uppercase letters.
9. New command
Javascript uses the new command to generate a new object from the constructor.
VaR o = new myobject ();
In this way, the question is: Once you forget to add new, the this keyword in myobject will point to the global object. As a result, all the variables bound to this will be programmed with global variables.
Rule 12: Use the object. Create () command
Rule 13: name of the constructor. uppercase letters are used. Other function names must be in lowercase.
10. Auto-increment and auto-subtraction Operators
The auto-increment and auto-increment operators are placed before or after the variables. The return values are different and are prone to errors.
All ++ operators can be replaced by "+ = 1 ".
Rule 14: Do not use the auto-increment and auto-increment operators, instead of the + = and-= operators.
11. Block
If the code body for loop and judgment has only one line, JavaScript allows this block to omit braces.
Rule 15: Always use braces to represent Blocks

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.