JavaScript code specifications
All JavaScript project applies the same specification.
JavaScript code specifications
Code specifications generally include the following aspects:
- Naming variables and functions
- Spaces, indentation, comments, usage rules.
- Other common specification /en/en/en
Standardized code easier to read and maintain.
Codes of general provisions in the development of the former, you can with your team members to negotiate the setting.
variable name
Variable names recommended method camel named (camelCase):
lastName = "Doe";
price = 19.90;
tax = 0.20;
fullPrice = price + (price * tax);
Space and the operator
Typically operator (= + - * /) to add a space before and after:
Example:
var values = [ "Volvo", "Saab", "Fiat"];
Code indented
Symbols typically use four spaces to indent a block of code:
function:
return (5/9) * ( fahrenheit - 32);
}
Not recommended to use the TAB key to indent, as different editors TAB key is not the same resolve. |
Statements Rule
Simple statements of general rules:
- Usually as a statement terminator symbol.
Example:
var person = {
firstName: "John",
lastName: "Doe",
age: 50,
eyeColor: "blue"
};
General rules for complex statement:
- The brace on the left end of the first row.
- Add a space before the curly brackets.
- The brace on the independent line.
- Do not end with a semicolon a complex statement.
function:
return (5/9) * ( fahrenheit - 32);
}
cycle:
x + = i;
}
Conditional statements:
greeting = "Good day";
} Else {
greeting = "Good evening";
}
Object Rule
Object-defined rules:
- The left brace on the same line with the class name.
- Between the colon and the attribute values are spaces.
- Use double quotes strings, numbers do not.
- The last attribute - value on the back do not add commas.
- The brace on the independent line, and symbols as the end symbol.
Example:
firstName: "John",
lastName: "Doe",
age: 50,
eyeColor: "blue"
};
Short object code can be written directly to the line:
Example:
Less than 80 characters in each line of code
For ease of reading recommend less than the number of characters per line 80.
If a JavaScript statement is more than 80 characters, it is recommended after the comma operator or wrap.
Example:
"Hello w3big.";
Naming Rules
Usually a lot of code language naming rules are similar, for example:
- Variables and functions for the hump Act (camelCase)
- Global variables uppercase (UPPERCASE)
- Constants (such as PI) uppercase (UPPERCASE)
Variable name you use these types of rules: hyp-hens, camelCase, or under_scores?
HTML and CSS dash (-) characters:
HTML5 attribute may data- (such as: data-quantity, data-price) as a prefix.
CSS uses - to connect the property name (font-size).
- Generally considered a subtraction in JavaScript, it is not allowed. |
Underline:
Many programmers prefer to use an underscore (eg: date_of_birth), especially in the SQL database.
PHP language usually use underscores.
Pascal spelling (PascalCase):
Pascal spelling (PascalCase) in C language more.
Hump law:
JavaScript is usually recommended hump method, jQuery and other JavaScript libraries use camel law.
Variable names do not begin with $ mark conflicts with a lot of JavaScript libraries. |
HTML load external JavaScript files
Use simple format to load JavaScript file (type attribute is not required):
Use JavaScript to access HTML elements
A bad HTML format may cause the execution of JavaScript errors.
The following two JavaScript statements to output different results:
Examples
var obj = getElementById ( "demo" )
HTML and JavaScript try to use the same naming rules.
Access HTML (5) code specifications .
File extension
HTML file suffix can be (or r .htm).
CSS file extension is .css.
JavaScript file suffix .js.
Lowercase filenames
Most Web servers (Apache, Unix) are case sensitive: london.jpg London.jpg can not access.
Other Web servers (Microsoft, IIS) is not case sensitive: london.jpg can be accessed via London.jpg or london.jpg.
You must maintain a unified style, we recommend consistent use lowercase file names.