JavaScript code specification recommendation

Source: Internet
Author: User

I. Naming
1. You should give the variables and functions a specific name.
2. Non-constructor uses the camper name method and adopts the dynamic object structure as much as possible to be different from the variable name, such as getName or IsFull. The first letter of the constructor (custom type) name is capitalized, which is different from that of the non-constructor, for example, Person.
3. Variables use the camper name method. Because JavaScript is a weak type language, we recommend that you prefix the variable name with: INTEGER (I), floating point number (f), Boolean (B), string (s ), array (). However, if you do not want to do this, you can choose based on your personal interests. After you have selected the option, do not mix the prefix and no prefix methods.

Ii. Layout
1. Space.
A) Leave a space between var and the variable name, a space between the variable name and the equal sign, a space between the equal sign and the initial value, and no space between the initial value and the semicolon. For example, var I = 10;
B) when a referenced type variable is declared literally, no space is left between each attribute and the colon, and a space is left between the colon and the initial value. For example:
Copy codeThe Code is as follows:
Var Person = {
Age: 16,
Name: "Sam"
};

C) Leave a space between the function and the function name. There is no space between the function name and (), and a space between () and.
D) Leave a space between the parameters of the function.
E) Leave a space between if, while, for, and left parentheses to emphasize the keyword. No space is left between switch, with, and left parentheses.
F) Leave a space between the binary operator and the Left and Right operands. When a line of code is too long, no space is left.
2. line feed.
A) each statement occupies one row. Do not use multiple statements for one row.
B) braces after block-level scopes such as if, while, and for {do not place a new row on the same line of keywords.
3. indent.
A) four spaces are used for indentation. Do not use tab.
B) if the scope is different, indentation should be performed to display its hierarchical relationship.

3. Notes
1. Add comments reasonably. Annotations cannot be completely absent, or the more annotations, the better. Add comments to important methods, variables, and algorithms (or other issues needing attention.
2. When modifying the source code, you must modify the comments synchronously to ensure consistency between the two.
3. Do not use html annotations in the code.

Iv. Specifications
1. When declaring a variable, the var keyword must be added. Although JavaScript does not allow the var keyword to be used as a global variable, this is a source of problems.
2. variables must be initialized at the same time. It is better not to change the data type of the variables later.
3. If you can add points at the end of a statement, you must add points.
4. if, while, for, and so on only one statement must be placed in braces.
5. Do not use global variables at will. If you have to use them, you 'd better use only one global variable.
6. JavaScript should be loosely coupled with html and css. Html is the data layer, css is the presentation layer, and JavaScript is the behavior layer. Close coupling should be avoided. Otherwise, it will be difficult to maintain it later. Do not include any specific JavaScript code in html. Use the method that contains external files. Do not use innerHTML or other methods to insert a large number of html elements in JavaScript. You should consider placing the elements in html, it only needs to be hidden initially. In JavaScript, do not directly modify the specific attributes in css, but indirectly modify them through className.
7. Do not modify objects that are not owned by you, add properties or methods to their instances or prototypes, or define existing methods repeatedly. Otherwise, an attribute or method with the same name is added to the new version of the object, which may cause unnoticeable problems. There are two solutions: inheritance and inclusion.
8. Use a namespace to prevent conflicts between multiple libraries. For details, refer to the YUI library organization method.
9. Put the literal in the code in the attributes of a variable, and the first letter or all letters of the attribute should be capitalized (simulate define or enum in other languages ). For example:
Copy codeThe Code is as follows:
Var Color = {
RED: 1,
BLUE: 2,
GREEN: 3
};

10. Check the parameters passed in the function. If it is a basic type, use typeof. If it is a reference type, use instanceOf. to check whether an object contains a method, use the typeof operator for the method and compare it with the string "undefined.

V. Performance
1. Avoid global search. The overhead of using global variables and functions is greater than that of using local variables and functions, because global variables and functions involve scope chain searches. Therefore, when a global variable is used multiple times in a function, it will be searched for Multiple scopes. To avoid this problem, you can assign a value to a local variable for multiple times, this local variable will be used later.
2. Avoid using the witch statement. With statements create their own scopes, resulting in additional overhead.
3. Avoid attribute search. Attribute search is an O (n) operation. Any attribute search on an object takes more time than the access variable and array (the access variable and array are O (1) operations ). Therefore, if the same attribute is used multiple times, it should be saved in a local variable. For example:
Copy codeThe Code is as follows:
Var sUrl = window. location. href;
Var sData = sUrl. substring (sUrl. indexOf ("? "));

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.