JavaScript Basic Concepts 1

Source: Internet
Author: User
Tags reserved

Beauty would fade but Love would stay.

ECMA-262 describes all the basic concepts of JavaScript through ECMAScript. Before we get to the basics of how JavaScript works, let's review the history of ECMAScript:
1) What does JavaScript have to do with ECMAScript?
JavaScript has much more meaning than ECMAScript, and a complete JavaScript implementation consists of a three-part ECMAScript, DOM, and BOM, where ECMAScript is the core of JavaScript Heart.
2) what is ECMAScript? The
has two different versions of early javascript: JavaScript in Netscape Navigator and JScript in Internet Explorer 3, so ECMA launched the ECMASCRI Pt. ECMAScript provides the basic syntax and features of JavaScript, and since then the browser developers have started to use ECMAScript as the basis for their respective JavaScript implementations. ECMAScript is a normalized script language.
3) what is ECMA-262? The
ECMA-262 is a standard definition of the ECMAScript language, has no dependencies on the Web browser, and does not contain input and output definitions. Because the ECMAScript defined by ECMA-262 is only the basis of JavaScript, developers can build a more complete scripting language on top of that.

(1) ECMA-262 1th Edition: Essentially the same as the Netscape JavaScript 1.1, in addition to the browser-specific features, support for the Unicode standard (multi-language development), the object has become a peace platform independent. (June 1997)
(2) ECMA-262 2nd edition: Strictly consistent with iso/iec-16262 (i.e. ECMAScript), no additions, modifications and deletions. (June 1998)
(3) ECMA-262 3rd edition: It marks ECMAScript as a real programming language. After that, ECMAScript was silent for many years, until the standard work started again after Ajax became popular. (December 1999)
(4) ECMA-262 4th edition: Abandoned before release, but most of its content has been inherited by ES 6.
(5) ECMA-262 5th edition: Seeks to clarify the ambiguity in the 3rd edition and adds new features. (December 2009)
(6) ECMA-262 version 5.1: Strictly consistent with ISO/IEC 16,262:2011 Third Edition. (June 2011)
(7) ECMA-262 6th edition: Added a lot of necessary features (June 2015)
(8) ECMA-262 7th edition: Also known as ECMAScript 2016, added two new features. (June 2016)
(9) ECMA-262 8th edition: Also known as ECMAScript 2017, added character handling, error handling, and some new functions. (June 2017)

Although ECMAScript's new standards are constantly being pushed out, most browsers on the market are now compatible with ECMAScript 5, but the work often requires a low-compatibility version of IE, so the ECMAScript defined in ECMA-262 3rd is still the most browser-capable A version.

1. Syntax

ECMAScript's syntax draws heavily on the syntax of C and other C languages.

Case sensitive

Everything in ECMAScript (variables, function names, and operators) is case-sensitive.

Identifier

An identifier is the name of a variable, function, property, or parameter of a function that consists of one or more characters that meet the following criteria:

    • The first character must be a letter, an underscore, or a dollar sign
    • Other characters can be letters, underscores, dollar signs, or numbers

The letters can contain extended Unicode alphabetic characters (because Unicode encoding is used when saving identifiers), so you can theoretically use any of the characters in UTF-8 (for example, Chinese characters) to make identifiers, but we do not recommend this.
By convention, the ECMAScript identifier uses the Hump naming method (first letter lowercase, the first letter of the following word capitalized).
The keyword, reserved word, true, false, and Null cannot be used as identifiers.

Undefined is a property of the Global Object window, which is not writable (writable:false), meaning it cannot be re-assigned within the global scope (invalid assignment but no error);
And because undefined is not a keyword or a reserved word for JS, it can be used as an identifier in any scope other than the global scope (for example, in a method).

Comments

ECMAScript uses C-style annotations, including single-line comments (//) and block-level comments (/* */).

/*   * 这是多行注释,此行左边的星号无特殊意义,仅用于提高注释可读性  */
Strict mode

Strict mode is introduced in ECMAScript 5, which defines a different parsing and execution model for JavaScript, in which some of the indeterminate behavior in ECMAScript 3 will be handled and some unsafe operations will throw an error.
To enable strict mode throughout the script, you can add the following code at the top of the script:

"use strict";

This line of code is a compilation instruction that tells the JavaScript engine to switch to strict mode.
Similarly, to enable strict mode in a function, you can include this compilation instruction above the inside of the function.

Statement

The statement in ECMAScript ends with a semicolon, and if you omit the semicolon, the parser determines the end of the statement, and it is recommended that you do not omit the semicolon at any time. It is recommended to use a code block (that is, {}) in a control statement, even if there is only one line statement in the code block.

2. Keywords and reserved words

ECMA-262 describes a set of keywords that can be used to represent the start or end of a control statement, or to perform a specific operation. The keyword cannot be used as an identifier. Here are all the keywords for ECMAScript:

break       do          instanceof  typeofcase        else        new         varcatch       finally     return      voidcontinue    for         switch      whiledebugger*   function    this        withdefault     if          throw       delete      in          

Where debugger is the new keyword for ECMAScript 5, before which it is a reserved word.

ECMA-262 describes a set of reserved words that cannot be used as identifiers, although they may be used as keywords in the future, even though these reserved words are not used in ECMAScript. The following are all reserved words (that is, all of Java's keywords) defined by ECMAScript version 3rd:

abstract    enum        int         shortboolean     export      interface   staticbyte        extends     long        superchar        final       native      synchronizedclass       float       package     throwsconst       goto        private     transientdebugger    implements  protected   volatiledouble      import      public

ECMAScript version 5th is reduced to the following when running in non-strict mode:

class       enum        extends     superconst       export      import      

This means that developers in non-strict mode can use reserved words that are released as identifiers.

ECMAScript version 5th in strict mode is reduced to the following number of reserved words:

class       enum        extends     superconst       export      import      implementinterface   static      let         yieldpackage     private     protected   public

Here, let and yield are the new reserved words for ECMAScript 5th edition. ECMAScript version 5th also imposes restrictions on eval and arguments in strict mode, which cannot be used as identifiers or property names, but they are not entirely reserved words.

To ensure compatibility, the 3rd version of the reserved word plus let and yield, that is, all the reserved words that are currently present, is used as a reference for programming.
JavaScript Pre-defines a number of global variables and functions and should avoid having their names used as variable names and function names. Each particular JavaScript runtime environment (client, server side, etc.) has its own list of global properties, and we can use the Window object to understand the list of global variables and functions defined in client JavaScript.

In the JavaScript engine that implements ECMAScript version 3rd, using keywords as identifiers can cause "Identifier expected" errors, but using reserved words as identifiers does not necessarily result in the same error, depending on the different engines.

ECMAScript version 5th modifies the rules for keywords and reserved words: keywords and reserved words cannot be used as identifiers, but can be used as property names for objects. We do not recommend this, in order to maintain code compatibility.

3. Variables

ECMAScript variables are loosely typed (that is, they can hold any type of data), and each variable is simply a placeholder for holding the value.

To define variables, you can use the var operator, for example:
var message;
An uninitialized variable like this will hold a special value of undefined.
var message = "hello";
Initializing a variable like this doesn't mark it as a string type, it's just an assignment, so we can modify the type of a variable, but it's not recommended.
A variable defined with VAR is usually a local variable that is valid only in the scope where the variable is defined, and if the scope is the entire script, then it is a global variable.

You can define a global variable by omitting the var operator:
message = "hello";
A variable defined in this way is always a global variable, even if the statement is within a method.
Global variables are difficult to maintain in a local scope, and global variables are not defined before a scope is executed, so global variables should be used with caution.
In strict mode, it is not allowed to assign a value to an undeclared variable (that is, a global variable), otherwise a referenceerror error will be thrown.

4. Statement if statement

Syntax:if (conditional) statement 1 Else Statement 2
The condition is not necessarily a Boolean value, ECMAScript automatically calls Boolean () to convert it to a Boolean value
Execute statement 1 when the condition is true, otherwise execute statement 2

Do-while statements

Syntax: do{statement} while (conditional)
is a post-test loop statement, that is, execute a statement before the judgment, the condition is set to continue executing the statement, or exit the loop

While statement

Syntax: while(conditional) statement
is a pre-test loop statement, that is, the first judgment, and then according to the results of the execution of statements or exit loops

For statement

Syntax: for(initialize statement; condition; Extra statement) Statement
It is also a pre-test loop statement that defines an initialization variable before the loop starts, and executes a single line of extra statements at the end of each loop
A For loop is the same as a while loop, and it simply concentrates the loop-related code together

for-in statements

Syntax: for(variable in object) statements
is a precise iterative statement that can be used to enumerate the properties of an object
Each time a loop is passed, an attribute in the object is assigned to the variable, and all the properties are traversed once to stop looping
If the object to be traversed is null or undefined, the statement is not executed and the loop is terminated directly

Label statement

Syntax: Tags: statements
The defined label can be referenced by a break or continue statement

Break and Continue statements

For precise control of the code execution in the loop, break exits the entire loop immediately, and continue exits the current loop and continues from the top of the loop

With statement

Syntax: With(object) statements
Used to set the scope of code to a specific object
For example:

var a = local.name;var b = local.address;

can be abbreviated as:

with (local) {    var a = name;    var b = address;}

The WITH statement is not allowed in strict mode

Switch statement

Grammar:

switch (条件) {    case 情况1:语句    break;    case 情况2:语句    break;    default:语句}  

Essence is a simplified if statement
Its judging condition can use any data type, case value is not necessarily a constant
Switch uses the all equals operator when comparing values, no type conversions occur

5. Functions

ECMAScript uses a function to encapsulate multiple statements, calling execution at any time anywhere
Basic syntax:

function 函数名 (参数列表) {    语句}

Functions are called with parentheses in the function name, the appropriate arguments can be passed in parentheses, and multiple arguments are separated by commas
A function in ECMAScript does not have to specify a return value type when it is defined, and the function can implement the return value at any time through a return statement
If the return statement does not have any return value, or if the function does not have a return statement, the function returns the undefined value
In strict mode, functions and arguments cannot be named eval or arguments, and parameters cannot have the same name

Understanding Parameters

ECMAScript does not mind passing the number of arguments and the data type, because the parameters are internally represented by an array, the parser does not validate the argument list, the named parameter is simply readable and not required, and the parameter array can be accessed through the arguments object
The length property of the arguments object indicates how many arguments are passed to the function, which allows the function to receive any number of arguments and implement the appropriate functions (such as overloading)
For example:

function add(num1,num2) { // num1 和 num2 是命名参数    return arguments[0] + arguments[1];}

The value of the parameter in arguments is always synchronized with the value of the corresponding named parameter (if there are named parameters), but they are not the same memory space that is accessed
If only one parameter is passed in, only arguments[0] and no arguments[1], but the named parameter with no value passed NUM2 will be automatically assigned the undefined value
In strict mode, the value of arguments does not allow overriding

    • Pass by value: the formal parameter of the function is a copy of the argument. Modifying the value of a parameter does not affect the argument. Due to the need to clone replicas every time, the performance of some complex types is low.
    • Pass by reference: the formal parameter of the function is an implicit reference to the argument. If the value of the parameter is modified, the argument is also modified. Makes tracing of function calls more difficult.

ECMAScript all parameter passing is a value pass, it is not possible to pass a parameter by reference because the value stored by the object variable is the memory address of the object in the heap memory.

    • Raw value: The Simple Type data segment stored in the stack, where the variable is accessed is where the value is stored.
    • Reference value: The object stored in the heap, the variable first accesses the value stored in the stack, gets a pointer (the memory address value in the heap), and then gets the object stored in the heap based on the pointer

The basic type of JS is passed by value.
The passing of a JS object is passed by share, and the function accepts the parameter referenced by the object argument, not a copy of the object passed by value, nor an implicit reference passed by reference.
Manipulating the properties of an object within a function is actually an action on the property that it points to the object, because both the formal parameter and the argument point to the same reference value, where the performance of shared delivery and reference passing is consistent.
However, if the whole of the parameter is manipulated, it is actually a newly defined object, and the value reference address of the parameter stored in the stack is modified for the reference address of the new object, when the formal parameter and the argument are independent of each other.

No overloads

The ECMAScript function cannot be overloaded because there are no signatures (the number and type of parameters that are accepted), but there are several ways to mimic the method overloads.
If you define two functions with the same name, the latter overrides the former.
The ECMAScript function parameter is an array of 0 or more values, which can be approximated by checking the number of parameters passed in to an overload of the function.

JavaScript Basic Concepts 1

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.