JavaScript Advanced Programming 1 ~ 3 part of the Knowledge points summary

Source: Internet
Author: User
Tags bitwise bitwise operators

Introduction to JavaScript in the first chapter

A brief introduction to JavaScript, some historical questions about JS.

JS early in different browsers there is a lot of compatibility problems, but slowly everyone is moving toward the standard, compatibility issues have been much better.

We usually hear ECMAScript, what does it have to do with JavaScript?

JS = ES + BOM (Browser object model) + DOM (Document Object model)

ES is the type of JavaScript, values, objects, properties, functions, and program syntax and semantics, the DOM is the ES run in the browser environment, the browser provides it with the extension to achieve more specific functions (Web browser is only one of the ES implementation of the Ken Environment, For example, node. js on the server). Developers can use the BOM to control the parts of the page that the browser displays.

But note that DOM is not for JS, and many other languages implement DOM as well.

While many browsers implement some well-known common features, other features may vary by browser.

Chapter II using JavaScript in HTML

The main way to use JS in a page is through the <script> element, there are two ways to use the <script> element

1. Embed the JS code directly in the page:

<script type= "Text/javascript" >

function Sayhi () {

Alert ("hi!");

}

</script>

However, when you use "</script>" in your code, you use the escape character "\", otherwise it will be treated as the end tag of the code.  such as alert ("<\/script>"); The browser will pop up "</script>".

2. Include external files:

<script type= "Text/javascript" src= "Example.js" ></script>

Label position

The browser will not start rendering the content until it resolves to the <body> tag, and the <script> element is placed in the

Therefore <script> is usually placed before the <body>,</body>.

All <script> elements are parsed sequentially in the order in which they appear in the page. Without using the defer and async properties, the code in the following <script> elements will not begin to parse until the code in the preceding <script> element is resolved.

Best practices for introducing JS code

Using an external file to introduce

Pros: Maintainable, cacheable, adaptable to the future

Document mode

The concept of document mode is implemented through the DOCTYPE switch, which initially consists of two modes: promiscuous and standard .

These two modes mainly affect the rendering of CSS content, in some cases it will also affect the interpretation of JS execution.

Then the IE proposed quasi-standard mode, and its differences with the standard mode can almost be ignored.

Chapter III Basic Concepts

identifiers : variables, functions, names of properties, or parameters of a function.

Rule: The first character must be a letter, an underscore (_), a dollar symbol ($), and other characters can be letters, underscores, dollar symbols, numbers .

The ES notation is accustomed to the hump notation, which is the first letter lowercase, leaving the first letter of each word capitalized: Firstsecond, MyCar, dosomethingimportant

Strict mode

Strict mode defines a different parsing and execution pattern for JavaScript.

In strict mode, some of the indeterminate behavior in ES3 is handled and throws an error on some unsafe operations.

In strict mode, JavaScript execution can be very different.

To use strict mode throughout your script, you can add code at the top

"Use strict";

Include this compilation instruction above the inside of the function to specify that the function executes in strict mode:

function () {

"Use strict";

Code

}

Variable

The variable of ES is loosely typed and can hold any type of data type

The var operator is used when defining variables (the const and let operators are added in ES6).

A variable defined with VAR becomes a local variable in the scope of the variable. If the var operator is omitted, the variable becomes a global variable.

Data type

ES has five simple types of data (basic type data): Undefined, Null, Boolean, number, String, and a complex data type Object

All values will be one of the above six data types

typeof operator

typeof is an operator and not a function.

There are six possible return values

1. "Undefined"

2. "Boolean"

3. "String"

4. "Number"

5. "Object"

6. "Function"

Note that typeof null returns "Object" because null is intended to be an empty object reference.

Undefined Type

This type has only one value: undefined

Variables that contain undefined values are different from undefined variables

VAR message;

var age;

alert (message); "Undefined"

alert (age); Error

But

typeof message and TypeOf age will return the undefined value, although it is not declared

Null type

This type has only one value: null, which represents an empty object pointer.

Boolean type

Two literals: True, False (true does not necessarily equal 1, false does not necessarily equal 0)

ES all types of values have values equivalent to these two values, you can call the transform function Boolean (), or do two non-operations (!!)

True, any non-empty string, any non-0 numeric value, any object will return True when a Boolean () is called ( including an empty array and an empty object [], {} )

Number Type

can represent integers and floating-point numbers

Octal, the first number is 0 (the octet is invalid in strict mode and throws an error)

Number.min_value holds the minimum value that ES can represent, which is 5e-324 in most browsers.

Number.MAX_VALUE 1.7976931348623157e+308

Values that are out of range are represented as Infinity and-infinity

NaN

Not a number

Two features: any operation involving Nan will return Nan, and Nan is not equal to any value, including its own

IsNaN () Any value that cannot be converted to a number will be returned true

numeric conversions

Number () and parseint ()

The former is more complex and unreasonable when converting strings, and the latter is more commonly used when working with integers.

parseint () when converting a string : It will first find the first non-whitespace character, if the first character is not a number or minus sign, the function immediately returns Nan (so the null character returns Nan).

If the first character is a number, the second character continues to be parsed until the next character is parsed or the first non-numeric character is encountered (the decimal point is treated as a non-numeric character).

parseint () can recognize various integer formats

parseint ("070"); (ES3) (ES5) (8 binary)

Parsetint ("0xf"); 15 (16 binary)

ES5 parseint () does not have the ability to parse octal values. To avoid that kind of problem.

The second parameter can be provided for parseint () (the cardinality used at the time of conversion, and the first parameter passed in will be parsed by this cardinality)

parseint ("AF", 16); 175

parseint ("AF"); NaN

You can then parse the octal, specifying that the cardinality will affect the output of the transformation.

Recommendation: The cardinality is explicitly specified under any circumstances.

parsefloat () also parses each character starting from the first character and resolves to the end of the character until the first invalid floating-point number character is encountered. Therefore, the first decimal point is valid, the second decimal point is invalid, and the string after the second decimal point is ignored.

parsefloat () parses only the decimal value (so the hexadecimal number is always resolved to 0), and no second parameter is established for the cardinality. Also, if the string contains a number that can be resolved to an integer (no decimal point or 0 after the decimal point), an integer is returned.

(An empty string will also return Nan)

String Type

Escape character: A character used to represent nonprinting characters or for other purposes. For example: \ n, \ t, \b, \ r

Character of the string

The string in ES is immutable, and once created, its value cannot be changed . To change the string saved by a variable, first destroy the original string , and then populate the variable with another string that contains the new value .

Convert to String

Numeric values, Boolean values, objects, and strings all have a toString () method, but null and undefined do not have this method.

Typically this method does not have to pass arguments, but when you call the ToString () method of a number, you can pass a parameter: the cardinality of the output value. The decimal is returned by default.

Attention

var a = 010;

A.tostring (); 8 (Chrome)

If you know that the converted value is not null or undefined, you can also use the Transform function string () to convert any type of value to a string: This method is called if there is a toString () method. For null returns "NULL", Undefined returns "undefined".

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

Object Type

If you do not give the constructor arguments, you can omit the subsequent pair of parentheses (var o = Object), but not recommended.

An important thought: In Es, the object type is the basis of all its instances, and any properties and methods that the object type has also exist in more specific objects.

Each instance of Object has the following properties and methods

    1. Constructor
    2. hasOwnProperty (PropertyName)
    3. isPrototypeOf
    4. propertyIsEnumerable (PropertyName)
    5. toLocaleString ()
    6. ToString ()
    7. ValueOf ()

operator

Self-increment, the decrement operator sets the value of the variable to NaN when the string does not contain a valid number, false is first converted to 0,true and is first converted to 1.

Unary Plus and minus operators: when a unary plus operator is applied to a non-numeric value, the value is transformed like the number () transformation function.

Bitwise operators :

The 32nd digit represents the symbol, 0 indicates a positive number, and 1 indicates a negative number.

When a bitwise operation is applied to a value, the interior is a 64-bit value that is first converted to a 32-bit value and then converted to a 64-bit value after the bitwise operation. When a bitwise operation is performed, NaN and Infinity are treated as a%.

The essence of bitwise non-operation is the assignment of operands minus one. There are also bitwise AND (&), bitwise OR (| ), bitwise XOR (^).

Shift Left: (<<) vacancy filled with 0

Signed Right Shift: (>>) vacancy is filled with a sign bit.

Unsigned Right Shift: (>>>) fills the vacancy with 0. The result of a negative unsigned right shift is usually very large.

Boolean operator

logical non (!): Returns false: Operand is an object, operand is a non-empty string, operand is an arbitrary number of 0 (including infinity)

Returns true: Operand is an empty string, 0, NULL, NaN, undefined

Tips: Using two logical non-operators, you will actually simulate the behavior of a Boolean () transformation function. The end result is the same as using the Boolean () function for this value.

logic and (&&): Returns one of two operands. Returns the second operand when the first operand is true. When the first operand is false, it returns the first operand directly. Logic and is a short-circuit operator, when the first operand can determine the result, it will not be evaluated for the second operand (even if the second value is not declared will result in an error).

logic or (| | ): Returns one of the two operands. When the first operand is true, the first operand is returned, and the first operand is false to return the second operand. He is also a short-circuit operator, the first operand is true, that is, when the first operand can determine the result, the second operand is no longer evaluated.

You can use this behavior to avoid assigning null values or undefined values to variables.

Multiply sex operator

This includes multiplication , division , and modulus (Take-off).

Additive operator

1. Addition: In the case that one operand is a string, the other operand is converted to a string, and then the two strings are stitched together.

2. Subtraction: If an operand is a string, Boolean, null,undefined, the number () function is called first in the background to convert it to a numeric value. If the result of the conversion is Nan, the result of the calculation is Nan.

If an operand is an object, call ValueOf () first, and if you get Nan, the result is Nan. Without the valueof () method, call the ToString () method to get the string and obtain the value.

Relational operations

<, <=, >= they all return a Boolean value.

1. Numerical comparisons are performed if they are numeric values.

2. The character encoding value is compared if it is all a string.

3. If one operand is a numeric value, the other operand is converted to a numeric value and then compared.

4. If an operand is a Boolean, convert it to a numeric value, and then compare.

5. Object. Call valueof () or the ToString () method before comparing.

"All" > "3"; False

"All" > 3; True Note conversion rules

NaN > 3; False

NaN <= 3; False

"A" < 3; False A is converted to NaN at this time

"A" >= 3; False

equality operators : Equality and inequality, congruent and non-congruent

1 Equality and inequality:

The two operators convert the operands before comparison (forced transformation) before comparing equality.

1. If a comparison value is a Boolean value, convert true to 1,false to 0 first.

2. An operand is a string, and an operand is a numeric value, the character is first converted to a numeric value.

3. One operator is an object, and the other is not, call the ValueOf () method, and the resulting base type value is compared in the previous method.

Rules

1.null equal to undefined

2. Before you compare equality, you cannot convert null and undefined to any other value.

3. If one of the numbers is Nan, the equality operator returns FALSE. Even though two numbers are Nan, the equality operator does not return true.

4. If the two operands are objects, the equality operator returns true only if the operands point to the same object.

2. Congruent and non-congruent

The equality operator returns true only if the two operands are equal without conversion. At this point, the null = = undefined returns false because they are of different types.

if ([]) {

Alert (1);

}

if ([] = = True) {

Alert (2);

}

if ([] = = False) {

Alert (3);

}

1

3

When an empty array is passed in directly, a Boolean () transformation function is called, and an empty array is an object that is converted to true so that the statements in curly braces can be executed.

However, when using the equality operator, the Boolean value is converted to a numeric value because it contains a Boolean value, which is called the number () transformation function, which is 0.

Function

Understanding Parameters

Arguments objects are just like arrays, not an instance of an array.

The value of arguments is always synchronized with the corresponding named parameter.

Notice: If only one value is passed in, the value set by arguments[1] will not be reflected in the named parameter. Named parameters that do not pass a value are automatically assigned the undefined value.

function Add (NUM1, num2) {

ARGUMENTS[1] = 10;

Alert (Arguments[0] + num2);

}

Add (10); NaN

Add (10,20); 20

Add (10,30); 20

cannot be overloaded.

JavaScript Advanced Programming 1 ~ 3 part of the Knowledge points summary

Related Article

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.