JavaScript core reading feeling of lexical structure _ basic knowledge

Source: Internet
Author: User
Tags control characters lowercase reserved

The lexical structure of a programming language is a set of fundamental rules that describe how you write the language. As the basis of the grammar, it prescribes what the variable name is, how to write the annotation, and how the statements are differentiated. This is a short space to introduce the lexical structure of JavaScript.

1. Character Set

JavaScript programs are written in the Unicode character set, which is a superset of ASCII and Latin-1 and supports almost all languages on the region. ECMASCRIPT3 requires that the implementation of JavaScript must support unicode2,1 and subsequent versions, ECMASCRIPT5 requires support for UNICODE3 and subsequent versions

I. Case sensitivity

JavaScript is a case-sensitive language, meaning that keywords, variables, function names, and all presentation characters must be in a consistent case, such as when the keyword while must be written as while and not as a while or while.

However, it is important to note that HTML is not case-sensitive (although XHTML distinguishes) and is easily confusing because it is tightly tied to client JavaScript. For example, in the handling event of an HTML setting, the onclick attribute can be written as onclick, but written as a lowercase onclick in JavaScript.

ii spaces, line breaks, and format controllers

JavaScript ignores spaces between the marks in the program (token), and in most cases JavaScript ignores line breaks as well. Because you can use space and line breaks freely in your code, you can create a unified coding style with neat and consistent indentation, which improves the readability of your code.

JavaScript in addition to identifying spaces (\u0020). JavaScript also Shi Cou characters that mark spaces as horizontal tabs (\u0009), vertical tabs (\u000b), page breaks (\u000c), nonbreaking whitespace (\u00a0), byte-order tokens (\ufeff), As well as characters in all ZS categories in Unicode. JavaScript recognizes the following characters as a Terminator: a newline character (\u000a), a carriage return symbol (\U000D), a row delimiter (\u2028), and a segment separator symbol (\u2029). Enter alphanumeric newline characters together. Resolves to a single line of end characters.

Unicode format control characters (cf classes), such as "Right-to-left writing Marks" (\u200f) and from "left to right writing marks" (\u200e), control the visual display of text. This is critical to the correct display of some non-English text, these characters can be in JavaScript annotations, string literals, and direct amounts of regular expressions, but not in identifiers (for example, variable names), but with an exception 0 wide connector (\u200d) and 0 wide non-connector (\ Ufeff) is a hand character that can appear in a marker but not as an identifier. As mentioned above, the byte-order tag format control character (\ufeff) is treated as a space

Iii. Unicode escape sequence

In some computer hardware and software, the complete collection of Unicode characters cannot be displayed or entered. To support those programmers who use old technology, JavaScript defines a special sequence that uses 6 ASCII characters to represent any 16-bit Unicode inner code. These Unicode escape sequences are prefixed with \u, followed by a hexadecimal mouse (using numbers and uppercase and lowercase letters a-f). This Unicode escape notation can be used in the direct volume of javascript strings, the Jiang Road of regular expressions, and the identifiers (except for keywords). For example, the Unicode escape of character E is written as \u00e9, and the following two JavaScript strings are exactly the same.

"Café" = = "Caf\u00e9" => true
Unicode escape writing can appear in comments, but because JavaScript ignores annotations, they are treated as ASCII characters in the context and do not receive the Unicode characters

IIII Standardization

Unicode allows you to encode the same character in a variety of ways. For example, character e can use Unicode character \u00e9, you can also use the normal ASCII character E to follow a intonation symbol \u0301, in a text editor, the two encodings display the same result, but their binary encodings are different and are not equal in the computer. The Unicode standard defines a preferred code format for indexed characters and provides a standardized way of translating text into a standard format suitable for comparison without standardizing other representations, strings, or regular expressions.

2. Note

JavaScript supports two annotations, and the text after the end of "//" is ignored by JavaScript as a comment.
In addition, the text between * and * is also used as a comment. This annotation can be written across lines, but cannot have nested annotations.

Single-line Comment
/*
*
*
*
*/
3. Direct Volume

The so-called direct quantity (literal) is the data value directly used in the program, the direct quantity is listed below

Copy Code code as follows:

12//Number
1.2//Decimal
"Hllo World"//String text
' Hi '//another string
True//Boolean value
False//Boolean value
/javascript/gi//Regular Expression Direct amount (used for pattern matching)
NULL//NULL

The 3rd chapter will explain in detail the number and the string direct quantity. The direct volume of regular expressions is explained in the 10th chapter. More welfare expressions can be written as arrays or as direct quantities of objects.

{X:1,y:2}//Object
[1,2,3,4,5]//array

4. Identifiers and reserved words

An identifier is a name. In JavaScript, identifiers are used to name variables and functions, or to be used as markers for jump positions in certain looping statements in JavaScript code. JavaScript identifiers must be in letters. Underline, or the dollar sign begins. Subsequent characters can be letters. Digital. Underline or dollar sign (numbers are not allowed to appear as a first letter, JavaScript can easily separate identifiers and numbers), and the following are valid identifiers

Copy Code code as follows:

My_variable_name
B13
_dummy
$str

In portability and ease of writing, we usually use only ASCII letters and numbers to write identifiers. It should then be noted that JavaScript allows for the presence of letters and numbers in the Fu Quan set of Unicode characters (the MN class, The MC class, and the P-Class) that ECMAScript allow the Unicode character mechanism to appear after the first character of the identifier. Programmers can use non-English language or mathematical symbols to write identifiers

Copy Code code as follows:

var sá= true;
Varπ= 3.14;

JavaScript takes some identifiers out of its own use as a keyword, so names cannot be used as identifiers in the program.

Copy Code code as follows:

Break
Case
Catch
Continue
Default
Delete
Todo
Else
Finally
For
function
If
In
instanceof
New
Return
Switch
This
Throw
Try
typeof
Var
void
While
With

JavaScript reserved word

Class Const Enum Export
Export extends Import super
In addition, these keywords are valid in normal JavaScript, but are reserved words in strict mode

Implements let private public yield interface package
protected static
The following identifiers are strictly restricted in the same strict mode, but cannot be done with variable names, parameter names, and function names.

Arguments eval
The specific implementation of JavaScript may define unique global variables and functions, and each particular JavaScript Runtime Environment (client) server has its own list of global attributes, which is to be borne in mind. (Window object to understand the list of global variables and functions defined in client JavaScript)

5. Optional semicolon

Like many programming languages, JavaScript uses semicolons (;) separate statements. This is important to enhance the readability and cleanliness of your code, the absence of a delimiter the end of a statement is the beginning of the next statement, and vice versa.
In JavaScript, the respective statements are exclusively one line, and you can usually omit the semicolon between statements (the semicolon that precedes the "}" curly bracket at the end of the program can also be omitted). Many JavaScript programmers (including the code examples in this book) use semicolons to explicitly mark the end of a statement. This is true even when the semicolon is not completely needed, and the other is omitted when any semicolon can be omitted, but only when it has to be used, regardless of the programming style, There are several details to note about JavaScript.
The following code, the first semicolon can be omitted

A=3;
b=4;
However, if you write in the following format, the first semicolon cannot be omitted.

a=3;b=4;
Note that JavaScript does not fill a semicolon at all lines of change: JavaScript fills a semicolon, in other words (similar to two exceptions in the following code), only if the code cannot be parsed correctly without a semicolon. If the current statement and subsequent non-whitespace characters cannot be parsed as a whole, JavaScript fills the semicolon at the end of the current statement, looking at the following code

var a
A
=
3
Console.log (a)
javascript resolves it to

var a;a=3;console.log (a);
JavaScript adds a semicolon to the first line, and without a semicolon, JavaScript cannot parse Var A in the code. The second a can be used as a single statement "A;", but JavaScript does not fill the semicolon at the end of the second line. Because it can be resolved with the third line of content into the "a=3;"

Some of the separate rules for some statements can cause unexpected situations, which are divided into two lines that appear to be two separate statements.

var y = x + F
(a+b). ToString ()
The second line of parentheses is composed of a function call with the first line of F, which JavaScript will consider as

var y = x+f (a+b). toString ();
Obviously the code is not meant to be, in order to allow the above code to parse into two different statements, you must manually fill out the display of the behavior of the semicolon

Generally speaking, if a statement starts with ([/+-), it is most likely to be parsed with the preceding statement, and the statement starting with/+-is not very common, but (the beginning statement is very common.) At least in some JavaScript encoder style is very common. Some programmers like to keep a semicolon in front of the statement, so that even if the previous statement was revised, the semicolon was mistakenly deleted, the current statement will be correctly resolved;
If the current statement and the next line of statements cannot merge parsing. JavaScript fills the semicolon after the first line, which is a universal rule, but has two columns. The first exception relates to returnm,birak,he continue statements, if these three keywords are followed by a newline. JavaScript fills the semicolon at the line break. For example

For example

Return
True
And JavaScript is parsed into

Return;ture;
And the code is meant to be

return ture;
That is to say return,break,contuine good subsequent expressions can not have a line, if added line, the program is very special circumstances to the error. and the debugging of the program is very inconvenient.

The second example involves the + +--operator, which can represent the prefix and suffix of an identifier expression. If it is followed by an expression, if it is used as a suffix expression. It and an expression should be seen as one line. Otherwise the end of the line will fill the semicolon.

Copy Code code as follows:

X
++
Yy

The above code resolves to

Copy Code code as follows:

X
++y

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.