3. Javacript advanced Programming-Basic concepts

Source: Internet
Author: User
Tags bitwise

1.1 Syntax

ECMAScript borrowed the syntax of C and other C languages

1.1.1 Case Sensitive

Everything in ECMAScript (variables, functions, and operators) is case-sensitive, and the variable test and test are different variables

1.1.2 Identifier

Identifiers are the names of variables, functions, and attributes, or parameters of a function. Identifiers can be one or more characters combined by the following formatting rules

(1). The first character must be a letter, an underscore, or a dollar sign

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

The letters in the identifiers can also contain extended ASCII or Unicode alphabetic characters

1.1.3 Notes

A single-line comment starts with two slashes, as shown below

Single-line Comment

Multi-line comments

/*

*

* Multi-line Comment

*

*/

1.1.4 Strict mode

To enable strict mode throughout the script, you can add the following code at the top of the

"Use Strict"

Include this compilation instruction above the function to specify that the function runs in strict mode

"Use Strict"

1.1.5 statements

var sum = 1 +2

var sum = 1+2;

Although the semicolon at the end of the statement is not required, it is recommended not to omit the end of the statement

1.2 Variables

Use the var operator when defining variables, as shown below

VAR message;

The defined variable award is defined as a local variable in the scope of the variable;

The variable defined by the omit var operator is a global variable, but it is not recommended to use this

Message

A defined variable can be used to hold any value

message = 10;

message = "HI";

1.3 Data types

There are 5 simple data types in ECMAScript: Undefined, Null,boolean,number, and string. There is also a complex data type Object

1.3.1 typeof operator

typeof is used to detect the data type of a given variable, and using the TypeOf operator on a value may return one of the following strings:

    1. "Undefined"-this value is undefined
    2. "Boolean"-This value is a Boolean value
    3. "String"-this value is a string
    4. "Number"-this value is numeric
    5. "Object"-this value is an object or null
    6. "Function"-this value is
1.3.2 Undefined type

The undefined type has only one value, which is undefined, and the variable defined with VAR is not assigned a value undefined

1.3.3 NULL type

A null type has only one value, which is null, which is used to represent an empty object pointer, which typically initializes the variable that holds the object to null

1.3.4 Boolean type

True or False two types

1.3.5 Number Type

The number type is used to represent numbers, which can represent integers or floating-point numbers

L Plastic

    1. Decimal

var num = 10;

    1. Octal: Starting with the number 0, invalid 8 binary data omits leading 0

var num = 070//8 Binary 56

var num = 089//Invalid 8 binary, 89

    1. 16 binary: Hexadecimal the first two bits must be 0x, invalid 16 will report syntax error

var num = 0xA//16 binary 10

L Floating point number

    1. Floating-point numeric value, which must contain a decimal point, and must have at least one digit after the decimal points
    2. For extremely small values, an e notation can be used

L Range of values

Maximum value number.max_value means that the maximum value is converted to infinity (positive infinity)

The minimum value of Number.min_value indicates that less than the minimum value is converted to-infinity (negative infinity);

You can use the Isfinite () function to monitor whether a value is between maximum and minimum

L NaN

Nan, which is a non-numeric value, indicates that an operation that returns a value does not return a numeric value, Nan is not equal to any numeric value, and can be used to determine whether Nan isNaN

L Numeric Conversions

    1. Number () function conversion
    • Ø if it is a Boolean value, True and false will be converted to 1 and 0, respectively
    • Ø if it is a numeric value, simply pass in and return
    • Ø if it is a null value, return 0
    • Ø if it is undefined, return Nan
    • Ø if it is a string, follow these rules:
    1. If it contains only numbers, it is converted to decimal, omitting the leading 0
    2. If a valid floating-point format is included, it is converted to a floating-point value, omitting the leading 0
    3. If a valid hexadecimal is included, it is converted to the appropriate decimal
    4. If the string is empty, it is converted to 0
    5. The others are converted to Nan
    • Ø if it is an object, then call the object's valueof () method, then follow the previous conversion rules, if the conversion result is Nan, then call the object's ToString () method, in accordance with the previous rules to convert
    1. parseint ()
    • The Øparseint () function ignores whitespace in front of the string when converting a string until the first non-whitespace character is found
    • Ø This function provides the second operator, the cardinality used when converting (that is, how many binary)
    1. Parsefloat ()
    • When the Øparseint () function converts a string, it ignores the space in front of the string until the first invalid floating-point character is found
    • Øparsefloat () recognizes all floating-point character formats
1.3.6 String Type

The string type is used to represent a sequence of characters consisting of 0 or more 16-bit Unicode characters, the string, which can be represented by double quotation marks (") and single quotation marks (').

(1). Escape sequences

Escape sequence Characters

\b Backspace

\f Paper Change page

\ nthe line break

\ r Enter

\ t transverse jump lattice (ctrl-i)

\ ' Single quotation mark

\ "Double quotation marks

\ \ counter Slash

\xnn a character represented by the hexadecimal code NN

\UNNNN A Unicode character represented by hexadecimal code nnnn

(2). The string is immutable

(3). String () function conversion

    1. If the value has the ToString () method, the method (with no arguments) is called and the corresponding result is returned
    2. Returns "NULL" if the value is null
    3. Returns "Undefined" if the value is undefined
1.3.7 Object Type

An object is actually a set of data and functions that can be created by executing the new operator followed by the name of the type of object to be created:

var obj = new Object ();

Each instance of object has the following properties and methods:

(1). Constructor: Saves the function used to create the current object

(2). hasOwnProperty (PropertyName): Used to detect whether a given property exists in the current object instance. where the attribute name (PropertyName) as a parameter must be specified as a string

(3). isPrototypeOf (object): Used to check if an incoming object is an incoming prototype

(4). propertyIsEnumerable (PropertyName): Used to check whether a given property can use the For-in statement to enumerate

(5). toLocaleString (): Returns the string representation of an object that corresponds to the region of the execution environment

(6). ToString (): Returns the string representation of an object

(7). VALUEOF (): Returns the string, numeric, or Boolean representation of an object. Usually the same as the return value of the ToString () method

1.4 operator 1.4.11-dollar operator

(1). Increment, decrement operator ++,--

(2). Unary Plus, minus operator +,-

1.4.2-bit operator

(1). Bitwise non ~, negative number of the intrinsic bitwise operator-1

(2). Bitwise AND &

(3). Bitwise OR |

(4). Bitwise XOR ^

(5). Left shift << original number multiplied by 2 the factorial of the left shift number does not affect the symbol

(6). The factorial of the left shift number of the right shift >> the original number divided by 2 does not affect the symbol

(7). Unsigned Right shift >>> move all 32 bits of a value to the right, including the number of symbols

1.4.3 Boolean operator

(1). Logical Not!

(2). Logic and &&

(3). Logical OR | |

1.4.4 Multiply sex operator

(1). Multiplication *

(2). Division/

(3). Modulo%

1.4.5 additive operator

(1). addition

(2). Subtraction

1.4.6 Relational operators

(1). Less than (<), greater than (>), less than or equal (<=), greater than or equal to (>=), returns a Boolean value

(2). The comparison rules are as follows:

    1. If two numeric values, perform a numeric comparison
    2. If two operands are strings, the character encoding value corresponding to the string is compared
    3. If one operand is a number, the other operand is converted to a numeric value for comparison
    4. If an operand is an object, the valueof () method of the object is called, the resulting value is compared according to the preceding rule, and if the object does not have a valueof () method, the object's ToString () method is called, and the resulting value is compared with the previous method.
    5. If an operator is a Boolean, it is converted to a numeric value for comparison
1.4.7 equality operator

(1). Equality and inequality = = =!

(2). Congruent and not congruent = = =!==

1.4.8 conditional operator

variable = boolean_expression?true_value:false_value;

1.4.9 assignment operator

(1). A simple assignment operator is represented by the equals sign (=)

var num = 10;

(2). Compound assignment operator

Multiply Assign value *=

In addition to the assigned value/=

Modulus Assignment%=

Add Value + =

Minus assignment =

Left Shift Assignment Value <<=

There is a compound right shift >>=

Unsigned Right Shift >>>=

1.4.10 comma operator

var num1=1,num2=2,num3=3;

1.5 Statement 1.5.1 If statement

if (condition) statement1 else statement2

1.5.2 do-while Statements

do {

Statement

}while (expression);

1.5.3 while statement

while (expression) statement;

1.5.4 for statement

for (initialization;expression;post-loop-expression) statement

1.5.5 for-in Statements

For (property in expression) statement

1.5.6 Break and Continue statements

Break and continue statements are used to precisely control the execution of code in a loop;

The break statement exits the loop immediately, forcing the statement following the loop;

Continue resumes from the top of the loop after exiting the loop

1.5.7 Switch statement

switch (expression): {

Case value1:

Statement

Break

Case Value:2

Statement

Break

Default

Statement

}

1.5.8 with statements

The function of the WITH statement is to set the scope of the code to a specific object

With (expression) statement;

1.5.9 Label Statement

Use the Label statement to add tags to your code for future use

Label:statement;

1.6 Functions

(1). Disclaimer

function functionname (arg0,arg1,arg2,..., ArgN) {

Statements

}

(2). A function that does not specify a return value returns a special undefined value

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

(4). Pass any number of arguments, and you can access them through the arguments object

(5). The function cannot be overloaded because there is no feature in the function signature

3. Javacript advanced Programming-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.