This article reviews the basic grammar in the ECMAScript specification, and good friends in English can read the official documents directly. JavaScript is essentially a Class C language, familiar with C language friends, can be very easy to read this article, and even can skip, but suggest you'd better take a look at the introduction of the same time, I may cite some think as difficult to understand and more popular usage.
Basic syntax
1, identifier: the so-called identifier, is actually refers to a certain specification, can be recognized by the engine name, can be used to represent constants, variables, function names, function parameters, objects, object properties, and so on all the names of the named objects.
(1) Case sensitive.
(2) begins with a letter, an underscore (_), or a dollar sign ($), and other characters can be letters, underscores, dollar signs, or numbers. The letters here contain extended ASCII or Unicode characters.
(3) Identifiers cannot be keywords, reserved words, true, false, or null. (Some browsers allow the use of undefined, some cannot).
(4) If the object attribute contains spaces or other special characters, it can be enclosed in parentheses as a whole.
2, Keywords: in the language itself has a specific purpose.
Break Case catch Continue debugger (added in ES5) default Delete does else finally for function if in instanceof new Return switch This throw try typeof var void while with
3, reserved words: By the language itself reserved, in the future may be as a keyword.
Reserved words in ES3:
Abstract Boolean byte char class const debugger Double enum export extends final float Goto implements Impo RT int interface Long native package private protected public short static super synchronized throws NT volatile
ES5 in the non-strict mode of the word:
Class Const Enum export extends import super
Reserved words in the strict mode of ES5:
Implements interface let (new in ES5) package private protected public static yield (new in ES5)
4, Strict mode: the introduction of strict mode in the ES5, by using "use strict" to open the strict mode, you can open the global strict mode at the top, or within the scope of the function to open local strict mode.
Copy Code code as follows:
"Use strict"//open global strict mode, in ES3, there will be no impact function fn () {
"Use strict"//open Local Strict mode
}
5, note: In ECMAScript, support two formats of annotations, Single-line comments and block-level comments:
Copy Code code as follows:
Single-line comment, with two slash//start/*
* Multi-line (block-level) annotation, preceded by a slash/and an asterisk *, an asterisk and a slash end, where the asterisk * is not required
*/
Note: As the JS code more and more complex, annotation also become more and more important, and document automation is becoming more important, there are many open source JS library for automated production similar to Javadoc JS documents, such as JSDoc, Yuidoc, etc., this time, on the annotation will have the corresponding format requirements, Interested friends can find relevant information to study.
6, Variable: variable in its essence is only memory space in the language level of the external abstraction.
(1) Dynamic type: In ECMAScript, variables are dynamic types, and you can initialize them to a number type at the time of definition, and then you can assign a string value to it:
Copy Code code as follows:
var age = 29;
Age = ' twenty-nine '; Despite this flexibility, I recommend that you do not do this unless you know exactly what you are doing.
(2) var operator: variable using VAR to declare, for uninitialized variables, the default is undefined, or you can use the variable without declaring (in my opinion, this is also a property without reason), the most important difference between them is the use of VAR declaration, A variable is defined in the global scope only if it is valid in the current scope, but not with var. The difference can be realized in the following example:
Copy Code code as follows:
var name = ' Linjisong '; Define global variables and assign values
Age = 29; Using variables directly is equivalent to defining global variables and assigning values
Sal Error
var salary; Define global variable, not initialized
Here just function declarations, no actual calls, so internally defined variables do not take effect
function fn () {
var name = ' Oulinhai ';//define local variables and assign values
Age = 23; Assigning values to global variables
Work = ' it '; var is not used, even if it is part of a function, it becomes a global variable
}
Before the function is actually called
Console.info (Salary); Undefined
Console.info (name); Linjisong
Console.info (age); 29
try{
Console.info (work);//Because there is no work defined in the global environment, an exception is thrown here
}catch (e) {}
FN ();//Actual call, changes to variables in code appear
Console.info (name); Linjisong, the global name value is not changed because VAR is used inside the function
Console.info (age); 23
Console.info (work); It
(3) Declaration of Ascension: This question will be mentioned again when speaking function declarations and function expressions, and here's a look at the code:
Copy Code code as follows:
Console.info (name);//undefined
Console.info (GetName);//getname () function
Console.info (GetName ());//undefined
try{
Console.info (age);//exception
}catch (e) {
Console.info (e);//referenceerror
}
Console.info (getage);//undefined
try{
Console.info (Getage ());//exception
}catch (e) {
Console.info (e);//typeerror
}
var name = ' Linjisong ';//variable declaration, elevation
Age = 29;//Use global variables directly, do not elevate
function GetName () {//Functions declaration, elevation
return name;
}
var getage = function () {//variable getage declaration, elevation; anonymous expression to get age, no elevation
return age;
}
Console.info (name);//linjisong
Console.info (GetName);//getname () function
Console.info (GetName ());//linjisong
Console.info (age);//29
Console.info (getage);//Get Age anonymous function
Console.info (Getage ());//29
Have you inferred the output from the above? If you have inferred that you can skip, and if you have questions, look at the following description of the elevation of the declaration, and then go back and verify the output:
A, the engine parsing, the first will parse the function declaration, and then resolve the variable declaration (parsing will not overwrite the type), and finally execute the code;
B, when parsing a function declaration, the type (function) is resolved at the same time, but will not execute, parsing the variable declaration, only the variable is resolved, not initialized.
The global scope is involved, and the function parameters in the function scope are also related to the declaration elevation, which is discussed later when the function is described.
The above code, first will be the 18th row of the function declaration and the 16th, 21 row of variable declarations to the most initial resolution, and then executed. So line 1th and 9 because the variable declaration is promoted but has not yet been initialized, so the output is undefined, thus the 11th row throws the type exception because it cannot be determined to be a function type, and the 2nd, 3 lines are raised by the function declaration and the function type is parsed, so the 2nd row outputs the function, But the return value is uninitialized and the output is undefined; the 5th row throws a reference exception because the variable has not been declared.
(4) You can use a single statement to define multiple variables, separated by commas. Such as:
Copy Code code as follows:
var name= ' Linjisong ',
Age=29,
work= ' it ';
(5) in the strict mode of ES5, you cannot define a variable named eval or arguments.
7, statements
(1) Statement: with a semicolon ";" At the end, if the semicolon is omitted, the parser determines the end of the statement.
For JS, the statement can omit the character of a semicolon, I can't think of any reason to exist, it is highly recommended that each statement use a semicolon to explicitly end, do not let the parser take the time to "guess" your program, and, more importantly, in many compression tools, speculation does not guarantee hundred percent correct.
(2) code block: Begins with a left curly brace ({) and ends with a closing curly brace (}).
In JS Although there is a code block concept, but there is no corresponding block-level scope, which is different from the General Class C language. For control statements (such as if), do not use a block of code because there is only one statement, which will sow the seeds of error for the guy who maintains your program.
Copy Code code as follows:
for (var i=0; i<10; i++)
{
}
Console.info (i)//output 10, after the code block can still access I, Description JS block-level scope
if (I < 10)
Console.info (i); No code block, easy to make mistakes when maintaining (such as adding 1 statements)
{
Console.info (i);
}
In addition to being used as a block of code, curly braces ({}) have an important use in defining object literals, which is discussed later.