Chapter III Basic Concepts

Source: Internet
Author: User

3.1 Syntax 3.1.1 Case sensitive

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

3.1.2 Identifier

Refers to the name of a variable, function, property, or parameter of a function.

Formatting rules:

The first character must be a letter, an underscore (_), a dollar sign ($);

Other characters can be letters, underscores, dollar signs, or numbers.

The ECMAScript identifier is in hump-case format, with the first letter lowercase and the first letter of each meaningful word remaining capitalized.

You cannot use keywords, reserved words, true, false, and NULL as identifiers.

3.1.3 Notes

Single-line Comment

/*

* Multi-line (block level) comments

*/

3.1.4 Strict mode

Add the following code at the top of the script: "Use strict".

3.1.5 Statements

The statements in the ECMAScript end with a semicolon;

Use curly braces to combine multiple statements into a block of code;

Code blocks are always used in control statements.

3.2 keywords and reserved words

ECMA-262 describes a set of keywords that can be used to indicate the start or end of a control statement, or to perform a specific operation, for a specific purpose:

Break does instanceof typeof case Else new Var catch finally return void continue for switch while debugger function this WI Th default if throw delete in try

Reserved words, which may be used as keywords in the future:

abstract enum int short Boolean export interface static byte extends long super Char final native synchronized class float Package throws const GOTO private Transient debugger implements protected volatile double import public let yield

Keywords and reserved words cannot be used as identifiers.

3.3 Variables

Ecmascrit variables are loosely typed and can be used to hold any type of data, and each variable is simply a placeholder for saving it.

Define variable: var (keyword) variable name (identifier);

A variable defined with the var operation becomes a local variable in the scope that defines the variable;

You can use a statement to define multiple variables, separated by commas.

3.4 Data Type 3.4.1typeof operator

Detects the data type of a given variable.

3.4.2Undefined type

When you declare a variable with VAR but do not initialize it, the value of the variable is undefined.

Execution of the typeof operator on uninitialized and undeclared variables returns the undefined value.

3.4.3Null type

Null object pointer. As long as the variable that is intended to hold the object does not actually hold the object, you should explicitly keep the variable with a null value.

3.4.4Boolean type

Only two literals: true and false;

Call the Transform function Boolean () to convert a value to its corresponding Boolean value.

3.4.5Number type

1. A decimal point must be included in the float value, and there must be at least one digit after the point.

2. Numerical range: 5e-324~1.7976931348623157e+308;

Use the Isfinite () function to determine whether a value is poor.

3.NaN: Non-numeric, the IsNaN () function determines whether the parameter is not a numeric value.

4. Numeric Conversions:

The number () transformation function can be used for any data type;

parseint () and parsefloat () are designed to convert strings into numeric values.

3.4.6String type

A sequence of characters consisting of 0 or more 16-bit Unicode characters, that is, a string. The string can be represented by double quotation marks ("") or single quotation marks (' ').

1. Character literals

2. Characteristics of strings

The strings in the ECMAScript are immutable;

3. Converting to a string

ToString () method; null and undefined no such method

Transformation function string (); Ability to convert any type of value to a string

To convert a value to a string, you can use the plus operator to add it to a string ("").

3.4.7Object type

is a set of data and functions;

var o = new Object ();

Object has the following properties and methods:

Constructor: Holds the function used to create the current object;

hasOwnProperty (PropertyName specified as a string): detects whether a given property exists in the current object instance;

isPrototypeOf: Checks if the incoming object is a prototype of another object;

propertyIsEnumerable (PropertyName): Checks whether the given property can be enumerated using the For-in statement;

toLocaleString (): Returns the string representation of an object, corresponding to the region of the execution environment;

ToString (): Returns the string representation of the object;

ValueOf (): Returns the string, numeric, or Boolean representation of an object;

3.5 Operator: For manipulating data values 3.5.11-Dollar operator

An operator that can manipulate only one value is called a unary operator.

1. Increment and decrement operators:

Pre-type: The value of the variable is changed before the statement is evaluated;

Post-type: The increment-decrement operation is performed after the statement that contains them is evaluated;

Follow these rules:

1, a string containing a valid number: first converted to a numeric value, plus minus 1;

2. A string that does not contain a valid numeric character: a value of Nan;

3, false: first converted to 0 plus minus 1;

4, true: first conversion to 1 plus minus 1;

5, floating point value, plus minus 1;

6. Object: Call valueof () to get an actionable value and apply the preceding rule to the value, and if the result is Nan, apply the aforementioned rule after calling ToString (). The object variable becomes a numeric variable.

2. Unary plus and minus operators

Applies to non-numeric values when the conversion is performed on this value.

3.5.2-bit operator

Manipulate the values by the bits in memory that represent the values. The 64-bit value is first converted to a 32-bit integer, then the operation is performed, and the result is then converted back to 64 bits.

For signed integers, the first 31 bits represent the value of the integer, and the 32nd bit represents the numeric symbol (symbol bit): 0 for positive numbers and 1 for negative numbers. Positive numbers are stored in a pure binary format; Negative numbers use twos complement (the binary of the numeric absolute value first, then the binary inverse code, and the inverse Code plus 1);

1, (~) bitwise non, return the value of the inverse code, the essence is the negative value of the operand minus 1;

2, (&) bitwise AND, the two values of each bit alignment, all 1 is 1, 0 is 0;

3, bitwise OR (|), 1 is 1, all 0 is 0;

4, the bitwise XOR (^), the same is 0, the difference is 1;

5, left Shift (<<), does not affect the sign bit;

6, signed right Shift (>>), retain the symbol bit, the value of the sign bit to fill;

7, unsigned Right shift (>>>), filled with 0;

3.5.3 Boolean operator

1, logical non (!) ), which can be applied to any value in the ECMAScript, first converting the operand to a Boolean value, and then negation.

2, Logic and (&&), full true is true, there is false, the short-circuit operator, the first false will not seek the value of the second number;

In the case where an operand is not a Boolean value:

1, the first operand is the object, the second operand is returned;

2, the second number is the object, only if the first number is true to return the object;

3, two numbers are objects, return the second one;

4, NULL, returns null;

5, there is Nan, then return nan;

6, have undefined, then return undefined;

3. Logic or (| | ), Ture is true, full false is false, the first number is true, then the value of the second number is not asked;

One of the operands is not a Boolean value:

1, the first operand is the object, returns the first operand;

2, the first number is false, returns the second number;

3, two numbers are objects, return the first one;

4, NULL, returns null;

5, there is Nan, then return nan;

6, have undefined, then return undefined;

3.5.4 Multiply sex operator

Automatic type conversions are performed in cases where the operand is not a numeric value.

1. Multiplication (*)

Special rules:

1, the operand is the numerical value, performs the conventional multiplication computation;

2, there is Nan, then nan;

3, Infinity by 0, for Nan;

4, Infinity by non 0, for Infinity or-infinity, depending on the symbol of the operation of the symbol;

5, infinity by infinity, for infinity;

6. If an operand is not a numeric value, call number () in the background to convert it to a numeric value, and then apply the rule above.

2. Division (/)

Special rules:

1, the operand is the numerical value, performs the General division computation;

2, there is Nan, then nan;

3, Infinity was infinity except, for Nan;

4, non 0 finite number is 0 except, for Infinity or-infinity, depending on symbol operand;

5, 0 except 0, is Nan;

6, Infinity by any non-zero except, for Infinity or-infinity, depending on the symbol of the operation of the symbol;

7. If an operand is not a numeric value, call number () in the background to convert it to a numeric value, and then apply the rule above.

3. Modulo (%)

Special rules:

1, the operand is the numerical value, performs the General division computation;

2, Infinity was infinity except, for Nan;

3, Infinity is limited by a large number of values in addition, for Nan;

4, not 0 A finite large number is 0 apart, for Nan;

5, is removed as 0, is 0;

6, the finite large number is infinity except, the result is the remainder;

7. If an operand is not a numeric value, call number () in the background to convert it to a numeric value, and then apply the rule above.

3.5.5 additive operator

1. Addition

1, numerical value plus numerical value, conventional calculation;

2, there is Nan, for Nan;

3, Infinity Plus infinity, for infinity;

4,-infinity plus-infinity, for-infinity;

5, Infinity plus-infinity, for Nan;

6, +0 plus +0, +0;

7,-0 plus-0, 0;

8, +0 Plus-0, +0;

9, String plus string, splicing;

10, the string plus non-string, the first non-string conversion into a string and then splicing;

2. Subtraction

1, numerical value plus numerical value, conventional calculation;

2, there is Nan, for Nan;

3, Infinity minus infinity, for Nan;

4,-infinity minus-infinity, for Nan;

5, Infinity minus-infinity, for Infinity;

6, +0 minus +0, +0;

7, 0 minus-0, +0;

8, +0 minus-0, is-0;

9,-infinity minus Infinity, for-infinity;

10, if there is an operand is a string, Boolean, null, undefined, first call the number () function to convert it to a numeric value, and then the previous rule calculation;

11, if there is an operand is an object, first call the valueof () function to obtain a value representing the object, if there is no valueof (), then call ToString () and convert the result to a numeric value, and then the previous rule calculation;

3.5.6 Relational operators

<, >, <=, >=

1, if the two operand is a numeric value, then perform a numerical comparison;

2, two strings, then the character encoding value is compared, the capital letter encoding is less than the lowercase letter encoding;

3, if the number of one operand, then the conversion of another number for the numerical comparison;

4, if there is an operand is an object, first call the valueof () function to obtain a value representing the object, if there is no valueof (), then call ToString () and convert the result to a numeric value, and then the previous rule calculation;

5, if a number is a Boolean value, then first converted to a numeric value in the comparison;

6, any operation number and Nan comparison result is false;

3.5.7 equality operator

1, equal (= =) and not equal (! =

Convert the operands first (forced transformation), and then compare their equality;

Conversion rules:

1, the Boolean value is converted to a value;

2, string and numeric comparison, string conversion to numeric value;

3, there is an object, first call valueof () and then compare;

4, null and undefined are equal;

5, Nan equals nan;

6, two object comparison is not the same object;

2, congruent (= = =) and not congruent (!) = =)

The operand is not converted before the comparison;

Congruent means that two operands are equal without conversion;

Non-congruent means that two operands are not equal without conversion;

3.5.8 conditional operator

Variable = boolean_expression? True_value:false_value;

3.5.9 assignment Operator (=)

Assign the value on the right to the left variable;

Compound assignment operators: *=,/=,%=, + =,-=, <<=, >>=, >>>=.

3.5.10 comma operator

You can perform multiple operations in a single statement, many for declaring multiple variables, and for assigning values that return the last item in an expression;

3.6 statements

ECMA-262 specifies a set of statements, also known as flow control statements. Statements define the primary syntax for ECMAScript, and typically use one or more keywords to accomplish a given task.

3.6.1if statements

Always use code fast (multiple lines of code enclosed in a pair of curly braces)

if (contidion1) {
Statement1
}else if (condition2) {
Statement2
}else {
Statement3
}
3.6.2do-while statements

Before a conditional expression is evaluated, the code in the loop body is executed at least once.

Do
{
Statement
} while (expression)
3.6.3while statements

The exit condition is evaluated before the code in the loop body is executed.

While
(expression) {
Statement
}
3.6.4for statements

The forward test loop statement.

For
(initialization; expression; Post-loop-expression optional) {
Statement
}
3.6.5for-in statements

is a precise iteration statement that can be used to enumerate the properties of an object.

For (property in
expression) {
Statement
}
3.6.6label statements

Add tags to your code for future use, typically in conjunction with loop statements such as for statements.

label:statement
3.6.7break and Continue statements

Used for precise control of the execution of code in the loop; the break statement exits the loop immediately, forcing the execution of the statement following the loop, and the continue statement exits the loop immediately and resumes execution from the top of the loop.

3.6.8with statements

The role is to set the scope of the code to a specific object.

With
(expression) {
Statement
}
3.6.9switch statements
switch (expression) {
Case Value:statement
break;
...
Default:statement
}
3.7 Functions
function functionname (arg1,arg 2,... Arg N) {
Statements
}
3.7.1 Understanding Parameters

Named parameters are provided for convenience only, but are not required.

The return value of the function is not required because any ECMAScript function can return any value at any time, and a function that does not specify a return value returns a special undefined value.

There is no concept of a function signature in ECMAScript because its function arguments are passed as an array of 0 or more values.

You can pass any number of arguments to the ECMAScript function and access those parameters through the argument object.

3.7.2 not overloaded

The function cannot be overloaded because there is no function signature attribute.

Chapter III Basic Concepts

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.