The essence of JavaScript language--grammar

Source: Internet
Author: User
Tags control characters

Guide: I see this book has been several times, every time will find a new continent, applauded! In order to further study again, but also to make this book into my handbook. Keep a good record of what you need, so you can review it over and over again. I wish I could go further and farther on this road.

First, the grammar

(1) Identifiers

Identifiers are preceded by a letter and can optionally be followed by one or more letters, numbers, or underscores.

Reserved words cannot be used, (this is written in my "deep understanding of JavaScript"--day1). In addition, undefined, Nan, and infinity, which should be reserved words, are also not available. However, the property name in the object literal can be used, but it needs to be quoted, and worse, it cannot be accessed using the dot (.), which can only be accessed using brackets ([]). Remember!

var a = {    ' nan ': ' Not a number '};console.log (a[' nan ');  // Not a number

So my advice is not to use reserved words is the best choice.

Note: Identifiers are used for statements, variables, parameters, property names, operators, and tags.

(2) Number

JavaScript has only one numeric type. It is internally represented as a 64-bit floating-point number. However, the integer type is not separated, so the values of 1 and 1.0 are the same.

① If the number literal has an exponential part, then the literal value is equal to the number before E and the number after E of 10. So 100 and 1e2 are the same.

② negative numbers can be in the predecessor operator-plus numbers.

③nan is a numeric value that represents a result of an operation that does not produce a normal result. Nan is not equal to any value, including its own. You can use isNaN to detect Nan.

The ④infinity represents all values greater than 1.79769313486231570e+308.

⑤ number ownership method. JavaScript has an object, math, that contains a set of methods that act on numbers.

(3) string

String literals can be wrapped in a pair of single or double quotation marks, which can include 0 or more characters. \ (backslash) is an escape character. When JavaScript is created, Unicode is a 16-bit character set, so all the characters in JavaScript are 16-bit.

There are no character types in ①javascript. To represent a character, you only need to create a string that contains only one character.

The ② escape character can be inserted into a string, such as backslashes, quotation marks, and control characters, that are not allowed in normal situations. The \u convention is used to specify numeric character encodings.

"A" = = = "\u0041"

The ③ string has a length property. For example, the length of "seven" is 5.

The ④ string is immutable. Once created, you can never change it. But you can use the + operator to link other strings to create a new string. Two strings that contain the same characters and have the same character order are also considered true.

' A ' + ' m ' = ' am ';

⑥ strings also have some methods.

(3) statement

A compilation unit contains a set of executable statements. In a Web browser, each <script> tag provides a compilation unit that is compiled and executed immediately.

① when a var statement is used inside a function, it defines the private variable of the function.

The ②switch, while, and do statements allow an optional predecessor label (label), which is used with the break statement.

③ statements are usually executed in order from top to bottom. JavaScript can change the execution sequence through conditional statements, loop statements, forced jump statements, and function calls.

The ④ code block is a set of statements wrapped inside a pair of curly braces. Unlike other languages, code blocks in JavaScript do not create new scopes. Therefore, the defined variables should be defined in the function's head, not in the code block.

The ⑤IF statement alters the program flow based on the value of the expression. The value of the expression is true when the code block followed is executed, otherwise the optional else branch is executed.

⑥ the following values are treated as false (false): ' (empty string), 0, false, null, undefined, NaN. All other values are true, including the string "false".

The ⑦switch statement executes a multi-path branch. It matches the value of its expression with all specified case conditions. Its expression may produce a number or a string. When an exact match is found, the statement in the matching case clause is executed. If no match is found, then an optional default statement is executed.

One of the case clauses can contain one or more case statements. A case expression must not necessarily be a constant. To prevent the next case from continuing, a forced jump statement should follow the subordinate clause. You can use break to exit the switch statement.

The ⑧while statement executes a simple loop. If the expression value is False, the loop is terminated. The expression is true, then the code block is executed.

The ⑨for statement is a more complex looping statement. There are two forms of it.

The first: It is controlled by 3 optional clauses: initialization statement, conditional clause, and increment clause.

The second type, called a for-in clause, enumerates all the property names of an object.

A ⑩do statement is like a while statement, and the only difference is that it detects the value of an expression after the code block executes instead of before. This means that the code block is executed at least once.

A. Try statement executes a block of code and captures any exceptions that the code throws. A catch clause defines a new variable to receive the thrown exception. The THORW statement throws an exception. If the throw statement is in a try code block, the control flow jumps to the catch statement. If the throw statement is in a function, the function call is discarded and the control flow jumps to the catch clause of the try statement that called the function.

The expression in a throw statement is usually an object literal, including a Name property and a message property. What the exception grabber can do with this information.

A. Return statement causes a premature return from the function. It can also specify the value to return. If no return value is specified, then undefined is returned.

Note: line wrapping between the return keyword and the expression is not allowed in JavaScript.

The break statement causes the program to exit a loop statement or a switch statement. It can specify one or two optional tags, and the exit is the statement with that tag.

Note: line breaks between the break keyword and the label are not allowed in JavaScript.

(4) expression

The simplest expression is a literal value (such as a string or number), a variable, a built-in value, a call expression starting with new, an attribute extraction expression starting with delete, an expression wrapped in parentheses, and an expression preceded by a predecessor operator. or followed by an expression:

--a middle operator and another expression (what is the central operator?) Check for a long time also can not find, I still look at the English version of the original it again?

--ternary operator? followed by an expression, then a: and then the 3rd expression

--A function call

--an attribute extraction expression

① Operator Precedence

. [] () Extracting properties and calling functions
Delete new typeof +-! Unary operators
* / % Multiplication, division, redundancy
+ - Addition/connector, subtraction
>= <= > < Inequality operators
=== !== Equality operators
&& Logic and
|| Logical OR
?:

Ternary

The arithmetic values of ②typeof are: number, string, Boolean, function, object, undefined. Where the null result is object, this is not true. I have said it before, then I will not explain it.

③ if! The value of the operator is true, then false is generated, otherwise true.

The ④+ operator can be used to add operator or string connections. If you want to add an operation, make sure that the two operands are numbers.

The ⑤/operator may produce a non-integer result, even if the two operands are integers.

⑥ if the first operand is false, && produces the value of its first operand, otherwise the value of the second operand is generated.

⑦ if the first operand is true, then | | Produces the value of the first operand, otherwise the value of the second operand.

The ⑧ function call throws the execution of the function. The function call operator is a pair of parentheses following the function name. The parentheses may contain arguments passed to the function.

⑨ A property access expression is used to specify an object or an array of properties or elements.

(5) literal

The ① object literal is a notation that makes it easy to create new objects by specified specifications. The name of the property can be either an identifier or a string. These names are used as literal names, so the object's property name is known at compile time. The value of the property is an expression.

The ② array literal is a notation that makes it easy to create arrays by specified specifications.

(6) function

The function literal defines the value of the function. It can have an optional name that is used recursively to invoke itself. It can specify a list of parameters that, like variables, are initialized by the actual arguments passed when they are called. The body of a function includes variable definitions and statements.

The essence of JavaScript language--grammar

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.