JavaScript Coding Specifications Recommended _ Basic knowledge

Source: Internet
Author: User
First, naming
1, should give variables and functions to take a precise name, do not arbitrarily named.
2, the constructor uses the hump naming method, uses the movable object structure as far as possible, distinguishes with the variable name, like GetName or Isfull. The constructor (that is, the custom type) is capitalized to distinguish it from the constructor, such as person.
3. The variable adopts the method of hump nomenclature. Because JavaScript is a weakly typed language, it is recommended to prefix the variable name: shape (i), floating-point number (f), Boolean (b), string (s), Array (a). But not forced to do so, according to the choice of personal preference, after the selection is not mixed with prefix and no prefix of these two ways.

second, the layout
1, space.
A a space between Var and the variable name, a space between the variable name and the equal sign, a space between the equals sign and the initial value, and no spaces between the initial value and the semicolon. such as: var i = 10;
b When declaring a reference type variable in a literal way, there is no space between each property and the colon, and the colon and the initial value are left blank. Such as:
Copy Code code as follows:

var person = {
Age:16,
Name: "Sam"
};

c The function is left with a space between the functions name and () without a blank space between () and {.
D) Leave a space between each parameter of the function.
e) Leave a space between the IF, while, for, and the left parenthesis to emphasize the keyword, the switch, with, and the left parenthesis.
f) The two-dollar operator leaves a space between the left and right two operands. When a line of code is longer, you can also leave blank spaces.
2. Change lines.
A each line statement occupies one line, not multiple statements line.
b if, while, for, block-level braces {Do not start another line, put it on the same line as the keyword.
3, indentation.
A) indent using 4 spaces, do not use tab.
b When scopes are different, indents should be performed to show their hierarchical relationships.

Third, note
1, reasonable add comments. Annotations cannot be completely without, not the more the better. Add comments to important methods, variables, and algorithms (or other issues that require attention).
2, modify the source code, you need to synchronize the comments, to maintain the consistency of the two.
3. Do not use HTML annotations in your code.

Four, norms
1, declare the variable must add the var keyword. Although JavaScript allows the Var keyword to become a global variable at this point, this is a source of problems.
2, the Declaration of variables must be initialized at the same time, it is best not to change the variable data type.
3, you can add a semicolon at the end of the statement, you must add a semicolon.
4, if, while, for, and so on only one statement, you also need to put in curly braces.
5, do not arbitrarily use global variables, if you have to use, it is best to use only a global variable.
6, JavaScript and HTML, CSS should be kept loosely coupled. HTML is the data layer, CSS is the performance layer, JavaScript is the behavior layer, the three should avoid tight coupling, otherwise it will be difficult to maintain later. Do not have specific JavaScript code in HTML, all the way to include external files; JavaScript also try not to use innerHTML to insert a large number of HTML elements, you should consider the elements in HTML, but initially hidden can , in JavaScript, do not directly modify the specific attributes in the CSS, but should be indirectly modified by classname.
7, do not modify not all of your objects, not to its instance or prototype add properties or methods, and do not repeatedly define their existing methods. Otherwise, when a new version of the object adds a property or method with the same name, it can cause potentially imperceptible problems. There are two kinds of solutions: one is inheritance, the other is inclusion.
8, the use of namespaces to prevent conflicts between multiple libraries, can refer to Yui Library organization.
9. For literal occurrences in code, you should place it in the properties of a variable, the first letter of the property name or all the letters in uppercase (simulate define or enum in another language). Such as:
Copy Code code as follows:

var Color = {
Red:1,
Blue:2,
Green:3
};

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

Five, performance
1, avoid global lookup. The cost of using global variables and functions is greater than using local variables and functions, because global variables and functions involve finding the scope chain. Therefore, when the global variable is used more than once in a function, a multiple scope chain lookup is performed, in order to avoid this problem, you can assign a global variable that is used more than once to a local variable and use that local variable later.
2, avoid the use of witch statements. The WITH statement creates its own scope, resulting in additional overhead.
3, to avoid property lookup. Property Lookup is an O (n) operation, and any property lookup on an object takes more time than accessing variables and arrays (access variables and Arrays are O (1) operations). Therefore, if you use the same property more than once, you should save it in a local variable. Such as:
Copy Code code as follows:

var surl = window.location.href;
var sData = surl.substring (Surl.indexof ("?"));
Related Article

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.