Latest web development tutorials
×

JavaScript Tutorial

JavaScript Tutorial JavaScript Introduction JavaScript usage JavaScript Output JavaScript grammar JavaScript Statement JavaScript Notes JavaScript variable JavaScript type of data JavaScript Object JavaScript function JavaScript Scope JavaScript event JavaScript The string JavaScript Operator JavaScript Comparison JavaScript If...Else JavaScript switch JavaScript for JavaScript while JavaScript Break & Continue JavaScript typeof JavaScript Type conversion JavaScript Regular Expressions JavaScript error JavaScript debugging JavaScript Variable promotion JavaScript Strict mode JavaScript Use errors JavaScript Form validation JavaScript Keep the keyword JavaScript JSON JavaScript void JavaScript Code specification

JS function

JavaScript Function definition JavaScript Function parameter JavaScript Function call JavaScript Closure

JS HTML DOM

DOM Introduction DOM HTML DOM CSS DOM event DOM EventListener DOM element

JS Advanced Tutorial

JavaScript Object JavaScript Number JavaScript String JavaScript Date JavaScript Array JavaScript Boolean JavaScript Math JavaScript RegExp Object

JS Browser BOM

JavaScript Window JavaScript Window Screen JavaScript Window Location JavaScript Window History JavaScript Navigator JavaScript Pop-ups JavaScript Timing events JavaScript Cookies

JS Library

JavaScript Library JavaScript test jQuery JavaScript test Prototype

JS Examples

JavaScript Examples JavaScript Object instance JavaScript Browser object instance JavaScript HTML DOM Examples JavaScript to sum up

JS Reference Manual

JavaScript Object HTML DOM Object

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 ......

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):

firstName = "John";
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 x = y + z;
var values = [ "Volvo", "Saab", "Fiat"];

Code indented

Symbols typically use four spaces to indent a block of code:

function:

function toCelsius (fahrenheit) {
return (5/9) * ( fahrenheit - 32);
}
Note 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 values = [ "Volvo", "Saab", "Fiat"];

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:

function toCelsius (fahrenheit) {
return (5/9) * ( fahrenheit - 32);
}

cycle:

for (i = 0; i < 5; i ++) {
x + = i;
}

Conditional statements:

if (time <20) {
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:

var person = {
firstName: "John",
lastName: "Doe",
age: 50,
eyeColor: "blue"
};

Short object code can be written directly to the line:

Example:

var person = {firstName: "John ", lastName: "Doe", age: 50, eyeColor: "blue"};

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:

document.getElementById ( "demo") .innerHTML =
"Hello w3big.";

try it"

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).

Note - 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.

Note 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):

<Script src = "myscript.js">

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" )

var obj = getElementById ( "demo" )

try it"

HTML and JavaScript try to use the same naming rules.

Access HTML (5) code specifications .


File extension

HTML file suffix can be .html (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.