Review the lexical structure _javascript techniques of JavaScript basic grammar

Source: Internet
Author: User
Tags reserved

Front.

JavaScript is a simple language, but also a complex language. It's simple because it takes only a moment to learn to use it, and it's complicated because it takes years to really master it. In fact, a front-end engineer largely refers to a JavaScript engineer. The front end is easy to master difficult to say is the front end, but also refers to the JavaScript. This article is the first piece of JavaScript basic syntax--lexical structure

With the Java relationship

On JavaScript there is a saying that the relationship between Java and JavaScript is the relationship between Lei Feng and Lei Feng Tower. Does it matter?

The first name for JavaScript is LiveScript, which later chose JavaScript as its official name, presumably to make it sound like a famous door. In addition to the syntax that looks like Java, JavaScript and Java are two completely different programming languages

The programming language is divided into two categories: interpretive type and compiling type. Languages such as Java or C + + require a compiler. Compilers are programs that translate source code written in high-level languages, such as Java, into files that are executed directly on the computer. Interpretive programming languages do not need compilers-they only need interpreters, and JavaScript interpreters in the browser will read directly into the source code and execute

Java can theoretically be deployed in virtually any environment, but JavaScript tends to be applied only to Web browsers. Also, in the JavaScript language, a function is a separate data type, with a prototype object (prototype)-based inheritance chain, JavaScript syntax is much freer than Java

Basically, the name JavaScript is intended to be "much like a Java scripting language."

Defined

JavaScript is a dynamic, weakly-typed, interpretive programming language that is ideal for object-oriented and functional programming styles. JavaScript's syntax derives from Java, its first-class function comes from scheme, and its archetype based inheritance comes from self

JavaScript is used to enhance the dynamic performance of the page, to achieve real-time, dynamic interaction between the page and the user

JavaScript is made up of three parts: ECMAScript, Dom, and BOM

[1] ECMAScript is defined by ECMA-262 and provides core language features (ECMA is the European Computer Manufacturers Association)

[2] DOM Document Object Model, which provides methods and interfaces for accessing and manipulating Web page content

[3] The BOM (Browser object model) Browser object models that provide methods and interfaces for interacting with browsers

Case sensitive

The characteristic of JavaScript, which cannot be overemphasized, is case sensitive. Keywords, variables, function names, and all identifiers in JavaScript must take a consistent case.

' Online ', ' online ', ' online ', ' online ' are four different variable names

Attention HTML is not case-sensitive

reserved word (Reservedword)

Like any other programming language, JavaScript retains some identifiers for its own use. These reserved words cannot be used as normal identifiers. As a result of many reference books misleading, seemingly reserved words and keywords are separate, in fact, is not the keyword is only a part of the reserved word. Reserved words include keywords, future reserved words, empty literals, and Boolean literal values

Reserved word Reservedword::

Keyword
Futurereservedword
Nullliteral
Booleanliteral

Key words

Break do instanceof typeof
Case Else New Var
Catch finally return void
Continue for switch while
Debugger function this with
Default if throw delete
In try

Future reserved Words

The following words are used as recommended extension keywords, so keep them so that future extensions may be used

class Enum extends Super
Const Export Import

ECMASCRIPT3 version

These are ECMAScript5 reserved words, but the reserved words in the ECMASCRIPT3 version are not the same, and if you want your code to run on an interpreter based on ECMASCRIPT3 implementation, you should avoid using the following reserved words as identifiers

Abstract Boolean byte char class constdouble enum export extends final float
GOTO implements import int Interfacelong native package private protected
Public short static super synchronized throw transient volatile

predefined variables and functions

In addition, JavaScript has predefined many global variables and functions and should avoid having their names used as identifier names

Arguments Array Boolean Date decodeuri decodeuricomponent encodeuriencodeuricomponent Error eval evalerror Function Infin ity isfinite
isNaN JSON Math NaN number Object parsefloat parseint rangeerror
Referenceerror RegExp String syntaxerror typeerror undefined urierror

Note (Comment)

Not all statements require a JavaScript interpreter to interpret and execute. Sometimes you need to write something in your script that you can only refer to or remind yourself of, and you want the JavaScript interpreter to be able to ignore the information directly, which is the annotation

Annotations can help you understand the code flow, and in code they play the role of a life note, helping us figure out what the script does

[note] Annotations must describe the code precisely, and no comment is worse than no comment

There are several ways to insert comments in JavaScript scripts, including Single-line comments, multiline comments, and HTML-style annotations

The "1" single-line comment starts with a two slash

Single-line Comment

The "2" multiline comment is also called a block-level comment, preceded by a slash and an asterisk///ending with an asterisk and a slash * *

/*
This is a multiline comment.
*/
[note] Those characters in block-level annotation/**/may also appear in regular expression literals, so block-level annotations are unsafe for annotated blocks of code
/*
var rm_a =/a*/.match (s);
*/

"3" HTML-style annotations apply only to single-line annotations, but the JavaScript interpreter does the same for <!--processing and/or processing

<!--This is a comment in JavaScript

If you are in an HTML document, you also need to end the comment with-->

<!--This is the annotation in HTML-->

But JavaScript does not require this, it treats--> as part of the annotation content

Attention HTML allows such annotations to span multiple lines, but each line of this annotation must be preceded by a "<!--" as a flag

<!--I'm note 1
<!--I'm note 2
<!--I'm note 3

Because the JavaScript interpreter differs from the HTML approach in handling this style of annotations, it is best not to use a JavaScript script in order to avoid confusion.

HTML-style annotations

Blank (whitespace)

Whitespace is usually meaningless, and sometimes it has to be used to separate character sequences, otherwise they are merged into a single symbol

var that = this;

The whitespace between Var and that is not removable, but other whitespace can be removed

JavaScript ignores spaces between identities (token) in the program. In most cases, JavaScript also ignores line breaks. Because you can freely use spaces and line wrapping in your code, you can make your code more readable by using neat, consistent indentation to form a unified coding style.

Increase code readability for
(var i = 1; i < i++) {
//

JavaScript will recognize these as whitespace characters whitespace

\u0009 Horizontal Tab <TAB>
\u000b Vertical Tab <VT>
\u000c page Breaks <FF>
\u0020 spaces <SP>
\U00A0 non-interrupt spaces <NBSP>
\ufeff character Fu She mark

JavaScript recognizes the following characters as line Terminators LineTerminator

\u000a line Break <LF>
\u000d return character <CR>
\u2028 Line Separator <LS>
\u2029 paragraph break <PS>

Optional semicolon

JavaScript uses semicolons, separating statements, which is important to enhance the readability and cleanliness of your code. But JavaScript does not fill the semicolon at all lines, and JavaScript fills the semicolon only if the code cannot be parsed correctly without a semicolon.

var a
A
=
3
Console.log (a)

javascript resolves it to:

var A;
A = 3;
Console.log (a);

The rules for separating this statement can cause unexpected situations.

var y = x + F
(a+b). ToString

javascript resolves it to:

var y = x + f (a+b). toString

Therefore, to allow the above code to parse into two different statements, you must manually fill in the end of the line with an explicit semicolon

Two exceptions

If the current statement and the next line of statements cannot be merged, JavaScript fills the semicolon after the first line, which is a universal rule, with two exceptions

The first exception to "1" is in scenes involving return, break, continue, throw statements. If the four keywords are followed by a newline, JavaScript fills the semicolon at the line break

Return
True

javascript resolves it to:

Return;true;

And the code is meant to be:

return true;

The second exception to "2" is that when the + + and--operator is involved, it should be the same row as the expression if it is used as a suffix expression. Otherwise, the end of the line will fill the semicolon, while + + or--will be the prefix operator for the next line of code and parse with it

X
++
Y

javascript resolves it to:

X;++y;

And the code is meant to be:

X++;y;

Although semicolons are not required, it is best not to omit them, because a semicolon can avoid many errors, and no semicolon at the end of the line can cause a compression error. Adding a semicolon also improves the performance of the code in some cases, because the parser doesn't have to take the time to speculate where the semicolon should be inserted.

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.