JavaScript advanced programming (version 3rd) Study Notes 2js basic syntax _ basic knowledge

Source: Internet
Author: User
This article will review the basic syntax in the ECMAScript specification. Good English friends can directly read the official documentation. JavaScript is essentially a type of C language. If you are familiar with C language, you can easily read this article or even skip it. However, it is recommended that you take a look at it, I may reference some common usage that I think is not easy to understand. This article will review the basic syntax in the ECMAScript specification. Good English friends can directly read the official documentation. JavaScript is essentially a type of C language. If you are familiar with C language, you can easily read this article or even skip it. However, it is recommended that you take a look at it, I may reference some common usage that I think is not easy to understand.

Basic syntax

1. identifier: the so-called identifier is actually a name that meets certain specifications and can be recognized by the engine, it can be used to represent the names of all callable objects, such as constants, variables, function names, function parameters, objects, and object attributes.

(1) case sensitive.

(2) It must start with a letter, underscore (_), or dollar sign ($). Other characters can be letters, underscores, dollar signs, or numbers. The letters here contain extended ASCII or Unicode characters.

(3) The identifier cannot be a keyword, reserved word, true, false, or null. (Some browsers allow undefined and some cannot ).

(4) If the object property contains spaces or other special characters, it can be enclosed in parentheses as a whole.

2. Keywords: the language itself has a specific purpose.

    Break case catch continue debugger (new in ES5) default delete do else finally for function if in instanceof new return switch this throw try typeof var void while

3. Reserved Words: words are reserved by the language itself and may be used as keywords in the future.

Reserved Words in ES3:

    Abstract boolean byte char class const debugger double enum export extends final float goto implements import int interface long native package private protected public short static super synchronized throws transient volatile

Reserved Words in non-strict mode in ES5:

    Class const enum export extends import super

Reserved Words in strict mode of ES5:

    Implements interface let (added in ES5) package private protected public static yield (added in ES5)

4. strict mode: the strict mode is introduced in ES5. You can enable the strict mode by using "use strict". You can enable the global strict mode on the top, you can also enable the local strict mode within the scope of the function.

The Code is as follows:


"Use strict" // enable the global strict mode. In ES3, function fn (){
"Use strict" // enable the local strict Mode
}


5. Notes: In ECMAScript, annotations in two formats are supported: single-line and block-level Annotations:

The Code is as follows:


// Single line comment, starting with two slashes /*
* Multi-row (Block-level) Comments start with a slash (/) and an asterisk (*), an asterisk (*) and a slash (*). The asterisk (*) in the middle of the line is not required.
*/


Note: As JavaScript code becomes more and more complex, annotations become more and more important, and document automation becomes more and more important. Currently, many open source JS libraries have been used to automatically generate JS documents similar to Javadoc, for example, JSDoc and YUIDoc. At this time, there will be corresponding format requirements for annotations. If you are interested, you can find relevant materials for research.

6. variables: variables are essentially external abstractions of memory space at the language level.

(1) dynamic type: In ECMAScript, variables are dynamic types. You can initialize them as a Number type during definition. Then, you can assign a string value to it:

The Code is as follows:


Var age = 29;
Age = 'twenty-nine'; // although this flexibility is available, I suggest you do not do this unless you know exactly what you are doing.


(2) var OPERATOR: variables are declared using var. For uninitialized variables, the default value is undefined. You can also directly use the variables without declaring them (in my opinion, this is also a feature with no reason). The most important difference between them is that when using var declaration, the declared variables are valid only in the current scope, but not using var, the variable is defined in the global scope. The following example shows the differences:

The Code is as follows:


Var name = 'linjisong'; // defines the global variable and assigns a value.
Age = 29; // directly use a variable, which is equivalent to defining a global variable and assigning a value.
// Sal; // Error
Var salary; // defines global variables, not initialized
// This is only a function declaration and is not actually called, so the internally defined variables will not take effect.
Function fn (){
Var name = 'external'; // defines local variables and assigns values.
Age = 23; // assign a value to the global variable
Work = 'it'; // var is not used. Even local variables in the function become global variables.
}
// Before the function is actually called
Console.info (salary); // undefined
Console.info (name); // linjisong
Console.info (age); // 29
Try {
Console.info (work); // an exception is thrown here because no work is defined in the global environment.
} Catch (e ){}
Fn (); // actual call. Changes to variables in the Code are displayed.
Console.info (name); // linjisong, because the function uses var internally, the global name value will not be changed.
Console.info (age); // 23
Console.info (work); // it


(3) Declaration escalation: This issue will be discussed again when talking about the function declaration and function expression. Here we will first mention it and look at the Code:

The Code is as follows:


Console.info (name); // undefined
Lele.info (getName); // getName () function
Console.info (getName (); // undefined
Try {
Lele.info (age); // exception
} Catch (e ){
Console.info (e); // ReferenceError
}
Lele.info (getAge); // undefined
Try {
Lele.info (getAge (); // exception
} Catch (e ){
Lele.info (e); // TypeError
}
Var name = 'linjisong'; // variable declaration to improve
Age = 29; // use the global variable directly without upgrading
Function getName () {// function declaration, upgraded
Return name;
}
Var getAge = function () {// declare the variable getAge to raise; obtain the anonymous function expression of the age without increasing
Return age;
}
Console.info (name); // linjisong
Lele.info (getName); // getName () function
Console.info (getName (); // linjisong
Console.info (age); // 29
Console.info (getAge); // gets an anonymous function of age.
Console.info (getAge (); // 29


Have you inferred the above output result by yourself? If it has been inferred, you can skip this step. If you still have questions, Let's first look at the following description about the Declaration elevation and then look back to confirm the above output result:
A. when parsing, the engine will first parse the function declaration, then parse the variable Declaration (the type will not be overwritten during parsing), and then execute the Code;
B. when parsing a function declaration, it will parse the type (function) at the same time, but it will not be executed. when parsing the variable declaration, it will only parse the variable and will not initialize it.
This involves only the global scope. In the function scope, function parameters are also related to Declaration elevation. We will discuss the function later.
In the above Code, we will first promote the declaration of the 18th-line function and the variable declaration of the 16th-line and 21-line to the first parsing, and then execute. Therefore, rows 1st and 9 are output undefined because the variable declaration is promoted but not initialized. Therefore, row 11th throws a type exception because it cannot be determined that it is a function type; 2nd and 3 rows because the function declaration is upgraded and the function type is parsed, so the 2nd row output function and the 3rd row can call the function, but the returned value is undefined, and the 5th row output is undefined because the variable has not been declared, A reference exception is thrown.
(4) You can use one statement to define multiple variables and use commas to separate them. For example:

The Code is as follows:


Var name = 'linjisong ',
Age = 29,
Work = 'it ';


(5) In ES5 strict mode, variables named eval or arguments cannot be defined.
7. Statements
(1) Statement: end with a semicolon ";". If the semicolon is omitted, it is determined by the parser that the statement ends.
The semicolon feature can be omitted for statements in JS. I do not think of any reason. It is strongly recommended that each statement end with a semicolon, don't let the parser take time to "Guess" your program, and, more importantly, in many compression tools, it cannot be correct.
(2) code block: Start with left curly braces ({) and end with right curly braces.
Although JavaScript has the concept of a code block, it does not have the corresponding block-level scope, which is different from the general C language. For control statements (such as if), do not use code blocks because there is only one statement. This will plant the seeds of mistakes for the guy who maintains your program.

The Code is as follows:


For (var I = 0; I <10; I ++)
{
}
Console.info (I); // output 10. You can still access I after the code block. This indicates that JS has no block-level scope.
If (I <10)
// Lele.info (I); when no code block is used, it is prone to errors during maintenance (such as adding one statement ).
{
Console.info (I );
}


In addition to being used as a code block, curly braces ({}) are also important to define the object literal volume, which will be discussed later.
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.