JavaScript Coding Specification Learning

Source: Internet
Author: User
Tags case statement

Http://javascript.crockford.com/code.html Article study notes.

1. Using JS file to manage code

All code is placed in the JS file as much as possible, and then introduced in the HTML file using script, which is introduced with the attention of the body tag and does not use type or language.

2, writing indentation

Indent with 4 blank spaces, and be careful not to indent with the TAB key.

3. Segmentation

Note that the president, each line not more than 80 characters, more than the appropriate segmentation, the segmentation should be followed by the operator, the most ideal is the comma (,) after the segmentation, the next line of segmentation after the use of 8 indentation.

4. Annotations

A single-line comment is generally used, and block annotations are generally used for documents.

5. Variable declaration

    1. All variables are declared before they are used, and undeclared variables are automatically used as global variables. The full text should use less global variables.
    2. It is a good idea to implement all the variable declarations with one Var, and each declaration is placed on a separate line, with a comment description, if all the declared variables are listed in character order, as follows:
    3. var currententry,      // current selection table item level    ;          // indent Level
    4. When all variables are defined at the top of the function body, Var appears in the first line of the function body.

6. Function declaration

  1. All functions should be declared before they are used, and-------after the variable to help you see the scope.
  2. The function names and parentheses should not have spaces directly, there should be no spaces between the right parenthesis (and the function arguments, the opening parenthesis), and the function body brackets {There is a space between the body, 4 spaces is indented, the function body ends brackets} and the function declaration keyword function First character alignment. The following code:
    1. function Outer (c,d) {    var e = c * D;
      function Inner (b) { return (E * a) + B; }
      return inner (0,1);}
  3. Functions and objects can be placed in any place where expressions are allowed to be placed.
  4. anonymous function keyword functions and opening parentheses (there is a space between them.)
  5. Use as few global functions as possible.
  6. For an immediate execution of a function, the entire invocation expression should be placed in a pair of parentheses () to clarify that the value of the variable is the result of the function execution rather than the function itself. The following code:
    var result = (function  () {    var key = "";      {        function  () {            return  key;        },         function (key) {            = key;}}    ;} ());

7. Naming

    1. Name by letter, number, underline, avoid using international characters, dollar sign $, backslash \.
    2. Do not use underscores as the first character of the name.
    3. Most variables and functions are named starting with lowercase letters.
    4. Constructors must start with uppercase letters, omitting new in JS will not error (compile or run errors), but it is best not to omit them.
    5. Global variables apply all uppercase names (there is no concept of macros and constants in JS).

8. Statements

    1. Simple statement
      1. Each line has a maximum of one statement, and a semicolon is used, and the statement that assigns values to function literals and object literals also uses semicolons;.
      2. JS allows any one variable as a statement, but when inserting a semicolon may cause some errors, so the general use of the expression statement is an assignment or a function call statement (the English text I probably understand, but do not know how to translate it better)
    2. Compound statement (a statement contained between a pair of {})
      1. The inner statement indents 4 spaces.
      2. The opening parenthesis {should be at the end of the start statement line.
      3. The closing parenthesis should be at the last individual line and aligned with the first character of the row that contains the opening parenthesis.
      4. When a statement is in a control statement (such as for, if, and so on), the statement should be surrounded with curly braces {}, even if there is only one statement, which guarantees that no bug is generated when the statement is added.

9, the label (this part of the understanding feeling is not very right)

The statement to use label is selective, with only the following: 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 with a newline).

11. If statement

Follow the following format:

(condition) {    Statements}
(condition) { { statements}
(condition) { else(condition) { { Statements}

12. For statement

Follow the following format:

(initiliazation; condition; update) {    Statements}
inch object) { (filter) { statements }}
    1. The first loop format is used for arrays and variables that can determine the number of iterations.
    2. The second is used for object traversal
      1. Note: It is mentioned here that the attributes added in the object's prototype can be enumerated, so the hasOwnProperty method is used for filtering, but when I use the for-in code test, it is not shown, where the problem is not known.

13. While statement

Follow the following format:

(condition) {    Statements}

14. Do-while Statement

Follow the following format:

{    statements  (condition);

Add a semicolon to the end of the statement.

15. Switch statement

Follow the following format:

(expression) { case expression:    statementsdefault:    statements }
    1. Each case should be aligned with the switch, avoiding excessive indentation, and only if the case tag is not a statement, it should not be indented.
    2. Each case statement (except default) must end with a break or a return or throw.

16. Try Statement

Follow the following format:

try {
Statements
} catch (variable) {
Statements
}

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

17. Continue Statement

Avoid using continue statements.

18. With statement

The WITH statement should not be used.

19, the use of space

    1. Enhance code readability by setting up empty lines to split logically related code snippets.
    2. Spaces are set in the following situations:
      1. Keyword followed by an opening parenthesis (to use spaces, for example:
      2. (true) {
      3. You cannot use spaces between function arguments and opening parentheses.
      4. The two-tuple operator, except for the dot (.), Brace bracket ((), and square brackets ([), is separated with an empty glyd and operand.
      5. There should be no space between a unary operator and his operand except for TypeOf.
      6. Each semicolon in the For statement control block (), followed by a space.
      7. There should be a space after each comma.

20. Additional recommendations

    1. [] and {}
      1. An array is used when the member name is a sequential integer, and the object is used when the member name is any string and name.
      2. Use {} Instead of new object () and [] instead of the new Array ().
    2. Commas, operators
      1. Avoid using commas, operators (this rule does not apply to object literals, array literal definitions and VAR declaration statements, and parameter lists)
    3. Block-level scopes
      1. In addition to conforming statements that do not use statement blocks, JS does not have block-level scopes, only function scopes.
    4. An assignment expression
      1. The Conditional Judgment section in the while and if statements avoids the use of assignment statements.
    5. = = = and!==
      1. The decision equality uses the congruent notation (= = = and!==), avoiding the use of mandatory type equality conversion symbols (= = and! =).
    6. If a number plus (or-) a number with a sign (+ or-), or a number with (+ + or-), you need to enclose the number with the symbol or (+ + or-).
    7. Eval is the Devil (eval's abuse L)
      1. Eval in the same situation, you should not use the function constructor and not pass a string to the settimeout or SetInterval function.

JavaScript Coding Specification Learning

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.