Advanced JavaScript programming fourth edition Chinese version

  Time flies, time came in 2020, while I was still the dish, even so, it is always in the inner desire to become a big old. As the saying goes master, round trip, I do not expect to be able to fly, but it will always be convinced that the day be able to fly. Also we hope that through this thing can be able to upgrade themselves in several ways. This is a series of articles, two weeks is expected to be able to update a chapter or two chapters, this, too, has achieved a flag, can remind ourselves that even if no matter how busy, but also to learn. On the other hand it is, because of my English for the bulk of English, some places still use a translator, may not be so accurate, but also want to be able to see the giant man able to point out to enable timely improvement and upgrading. Finally, JavaScript fourth edition of the domestic high-level programming like Turing's people still get in, if infringement, please propose to delete the series will not be used for commercial purposes, only to learn reference.

  The first two chapters of the third edition and no major changes, so I skipped.

The first part of the JavaScript base

In this chapter:


  1. Review basic grammar
  2. Data type of use
  3. Variable declaration
  4. Explain the function

  Any core language that is described on how to run the bottom, usually presents this description defines the syntax, operators, data types, and to provide a built-in functions can construct complex solutions. ECMA-262 as a pseudo language called JavaScript ECMAScript defined as all of this information.
  ECMAScript ECMA-262 is defined in the fifth edition of the web browser. The sixth edition is to be implemented in the next browser version, as of 2017, most major browsers have basic or fully implement the specification. For this reason, the following information is mainly based on the sixth edition of ECMAScript defined.

About Syntax

ECMAScript syntax borrowed heavily from C and other C-like language, such as Java and Perl. Developers familiar with this kind of language should be easy to grasp ECMAScript syntax looser.


About the case

  A need to understand the concept, all the contents are case sensitive; variable, function name, and other operators are case sensitive so it is easy to distinguish them to test Test is different. A similar situation also typeof can not be used as a function name, as is a typeof keyword. But in the back of a typeof is a valid function name.


Identifier

  Identifier is the name of the variable, the function, property, or function. Identifier may be in the form of one or more characters: 1, the first letter must be a letter, underscore (_) character or $. 2. All other characters can be letters, underscores, or $ characters digital.

The letter identifier may comprise an extended Ascll Unicode letters or characters, e.g. À and Æ to, but is not recommended for such use

  By convention, ECMScript identifier using camel nomenclature, the first character is lowercase, first letter after each word such as: FIRST SECOND
myCar
doSomethingImportant

Although this is not strictly required, but we should follow ECMScript follow this naming.

Note: keywords, reserved words, to true , false and null can not be used as identifiers. Please refer to the follow-up " Keywords and reserved words " section


Note

  ECMAScript for single-line use and annotation C block comments style. Single-line comments start with two forward slash character, for example:
// this is a double slash comment

block comments a forward slash and asterisk (/ *) and end at the opposite end (* /), as in this example:

/ * this is a comment block * /


Strict Mode

  ECMAScript 5 introduces the concept of strict mode, JavaScript under the strict mode is a different kind of parsing and execution mode, which dealt with some erratic behavior in 3 ECMAScript and some of the insecurity. If you want to turn on strict mode, you need to write on the top:
"use strict";
normal string looks like, but this is a flag that tells the JavaScript engine, the code needs to be converted into strict mode. The reason here write, in order to avoid unnecessary impact on ECMScript 3.
You can also use the specified function body strict mode:
function doSomething () {
"use strict"
// function body
}
strict mode changed the course of performing many of JavaScript, in order to distinguish, in the book, all of the strict mode are It will be pointed out.


statement

  ECMAScript to identify the end of a semicolon, but can be omitted, as follows:
the let SUM = A + B // write is not the problem, but this is not recommended
let diff = a - b; // recommended wording

  even at the end of the statement does not require a semicolon, it should always contain a semicolon. Write a semicolon help prevent errors of omission, such as not completed what you type, and allows developers to compress the code ECMAScript by removing extra spaces (when the line does not end with a semicolon, this compression will cause a syntax error). In some cases, may also improve performance include semicolons, because the position of the parser will pass a semicolon semicolon belongs to correct syntax errors
by using C style syntax, multiple statements may be combined into a code block , the left brace ({) begins, the right brace (}) end:
IF (Test) {
Test = to false;
the console.log (Test);
}

  control statements (e.g., if) only the plurality of statements when executed code block. However, it is always best to use blocks of code and control statements together, even if only one statement to be executed in the following example:
// write is no problem, but it is easy to make mistakes, so I do not recommend this writing
IF (the Test)
Console .log (Test);
// recommended wording
if (test) {console.log (test )}

of the possibility of error control statement blocks of code intended to make more clear, and need to be changed less


Keywords and reserved words

  ECMA-262 is described having a set of reserved keywords for a particular purpose, for example indicates that the control start or end of the statement or perform specific operations. According to the rules, keywords are reserved and can not be used as an identifier or attribute name. The sixth edition of ECMA-262 a complete list of keywords as follows:
BREAK , do , in typeof ,
Case
, the else , instanceof , var ,
the catch
, Export , new new , void
class
, the extends , return , the while
const
, a finally , Super , with
the Continue
, for , Switch , yield
Debugger
, Function , the this , default
IF
, the throw , the Delete , Import
the try

  specification also proposed a set of reserved words in the future can not be used as an identifier or attribute name. Although there is no specific uses reserved words in the language, but they retain for future use as a keyword.
The following is a complete list of reserved words in the future ECMA-262 defined in the sixth edition:
has been retained:
enum
reserved strict mode:
the implements, Package Penalty for, public, interface
protected, static, the let, Private
Reserved, in
retained in the code block of:
the await
those words still can not be used as identifiers, but can now be used as a property name object. In general, it is best to avoid using both keywords and reserved words as identifiers and attribute names, to ensure upward and downward compatible with the version of ECMAScript.


variable

  ECMAScript variable is loosely typed, which means that the variable can store any type of data. Each variable is simply a named value placeholder. There are three keywords that can be used to declare variables: var (available in all versions of ECMAScript) and const and let (introduced in ECMAScript 6 in).


Keyword: var

If you want to define a variable using var, var written in front (Note: var is a keyword), followed by the variable name (identifier described above), such as:
var the Message;
This code defines a named message, it can be used to hold any value. (If you do not initialize, leave the special value undefined, which will be discussed in the next section). ECMAScript implementation variable initialization, the variables can be defined and colleagues set values, for example:

var message = "Hi";
here we message variable defined to include a string value "hi", does not perform this initialization flag variable is a string type ; it's just assign values to variables. However, we can either change the value stored in a variable or change the type of worth, for example:
var the Message = "hi"; the Message = 100; // This is possible, but this is not recommended
in this Liezi, we first the initial value of the variable message is defined as "hi", then the value 100 is covered, even though we do not recommend it, but doing so is possible in ECMAScript


Scoped variable declaration

  It should be noted that the use of operator defined variables var making local variables defined scope variables. For example, using the variables defined internal function means var variable is destroyed when the function exits, for example:
function Test () {//
var Message = "Hi"; //
// local variable
}
Test ();
the console.log ( message);! // message is not defined

  here, we () function used in the test var declares a variable named message and initializes its value, followed by the variable is destroyed, so in this example the last line I will complain in the console. However, simply omitting var operator, can be globally defined variables, as follows:
function Test () {//
Message = "Hi"; // global variables
// local variables
}
Test ();
the console.log (Message ); // "hi"

by the operator from deleting var example, message variable becomes a global variable. Once the calling function test (), the variable will be defined, and can be accessed outside the function after execution.

Note: Although it is possible by omitting var defined global variables operator, but this method is not recommended. Local global variable definition is difficult to maintain, and can lead to confusion. When undeclared variable is assigned under strict mode reference error will be reported.

  If you need to define a plurality of variables, a single statement to complete, each variable (and optional initialization) separated by commas, such as:
var Message = "Hi",
    found = to false,
    Age = 18 is,

here, is defined and initialized three variables. Since ECMAScript loose type, it may be used in different types of variable initialization data into a single statement. While not need to insert line breaks and indentation variables, but it helps to improve readability. Running in strict mode, you can not define a variable named eval or arguments (although not a member of keywords or reserved words, but doing so will result in a natural syntax error).

 

 

 

Guess you like

Origin www.cnblogs.com/yang656/p/12330966.html