Comments (0)

Summary of JS detailed knowledge points

1. Ecmasctipt syntax

1.1 syntax introduction: JS code cannot be written directly and needs to be wrapped with script tags

JS is a weakly typed scripting language, that is, there are no strict requirements and restrictions on syntax. Variables of any type are declared with VaR, and variables of different types can be declared with a var.

1.2 reference to external JS files

Note: if the script tag refers to external JS, do not write JS script in the script tag, because the JS script inside will not be executed

1.3 JS basic data types:

1. Number: contains integer, floating point, Nan and infinity

2. Boolean: contains two values: true and false

3. Undefined: indicates the type of declared but unassigned variable

4. String: a collection of characters representing '' or '' packages

5. Null: null value. If a variable value is null, it is an empty object, and the type of null value itself is object

1.4 JS basic type conversion:

1. Cast type

3 conversion functions

1) Number(): converts a parameter to a number. If a part of the parameter is not a number, the conversion fails and Nan is returned

2) Boolean (value): convert the parameter to boolean type,

When value is 0, - 0, null, "", false, undefined, or Nan,

Then the value converted from Boolean () to boolean type is false.

3) String (value): converts a parameter to a string

2. Implicit type conversion

When two variables of different data types are + connected, how does the browser's JS engine interpret it?

The browser's JS engine will implicitly call the cast function to convert one value to the type of another value

If a string type is connected to another type, the default string type takes precedence, that is, another class will be converted to string type

3. Conversion function

Parseint(): converts the parameter to an integer. Only the numeric part of the parameter is converted, and the non numeric part is ignored. If none of the parameters is numeric, Nan is returned

Parsefloat(): converts a parameter to a floating-point number, only the numeric part of the parameter

1.5 pop up box:

1. Alert (): ordinary pop-up box, pure prompt function

2. Prompt (): a pop-up box with an input box pops up, allowing the user to input information

Parameter 1: prompt text on the dialog box parameter 2: default value in the input box

The return value is the value entered by the user

3. Cofirm (): pop up a pop-up box with OK and Cancel buttons to prompt the user whether to perform an operation

The return value is true or false. If the user clicks the OK button, it will return true. Otherwise, it will return false

1.6 operator:

===: all equals. True is returned only when the data types and values of the two operands are the same. Otherwise, false is returned

==: equal to, only compare the content (value), not the type, and return true as long as the content is the same

||: or, if both operands are Boolean, if the first parameter is true, the first operand will be returned directly

If the first parameter is false, the second number will be returned directly;

If both operands are not Boolean, the JS engine will implicitly convert the first parameter to boolean type and return true or false,

If the result of the first number conversion is true, the first number will be returned directly; If the first number is false after conversion, the second number is returned

1.7 switch selection structure:

The switch structure in JS meets the switch syntax in Java

Swtich (expression){

Case constant (integer, string):

Statement 1

break;

Case constant (integer, string):

Statement 1

break;

default:

If the above conditions are not met, the default statement is executed

break;

}

The switch structure in JS is different from Java in that it supports the addition of an expression after the case. The syntax is:

switch(true){

Case expression 1:

Statement 1

break;

Case expression 2:

Statement 2

break;

...

}

The above syntax is equivalent to the multiple if judgment structure if elseif... elseif... else

1.8 number of characters:

1.8. 1. Two ways to create strings in JS

1. Create with literal quantity, that is, 'xxx' or "XXX" var S1 ='Hello ';

2. Create with new string ('xxx ')

Common attribute: length: get the length of string var S2 = new string ("world");

1.8. 2 substr string interception: parameter 1 is where to start interception, and parameter 2 is the length of interception

1.8. 3 slice interception: parameter 1 is where to start interception, and parameter 2 is the end position of interception (excluding the end position)

1.8. 4 tolocaleuppercase: convert all strings to uppercase letters

1.8. 5 tolocalelovercase: convert all strings to lowercase letters

1.8. 6 split: convert string to array

1.8. 7 string encoding encodeuricomponent(): encodes the Chinese characters in the URL path and: /, and converts them to the form of alphanumeric%

1.8. 8 string decoding decodeuricomponent(): decodes the encoded string

1.8. 9 escape character: if double quotation marks are used after a pair of double quotation marks, the double quotation marks inside must be written as the corresponding escape character\“

1.9 array:

1.9. 1 JS array creation method:

Example: VAR Arr1 = [1, "a", true, undefined];

2. Create an array using the constructor new array (a, c)

Example: VAR arr2 = new array ("a", 1, true);

Note: the upper case of the array in JS can be modified. When the index is out of bounds, the array will automatically become larger. Access the elements whose subscript is out of bounds and return undefined

1.9. 2 array method

1. Add element push(): add one or more elements to the end of the array and return the new length of the array

2. Add element unshift(): add one or more elements to the beginning of the array and return the new length of the array

3. Delete element pop (): delete the last element and return the value of the deleted element

4. Delete element shift(): deletes the first element and returns the value of the deleted element

5. Add or delete the element splice (). The parameters are as follows:

Start: [required] integer specifying the position (inclusive) of adding / deleting items. If a negative number is used, the position can be specified from the end of the array

Deletecount: [required] the number of items to delete. If it is set to 0, the items will not be deleted.

Items: [optional] the new item added to the array can be one element or multiple elements.

6. Join(): converts an array into a string, which is the opposite of the split() of the string

2.0 functions:

Function: a repeatedly executed code block, which is equivalent to a method in Java

1. Function definition: function name (parameter 1, parameter 2... Parameter n){

Function body

[return value];

}

Different from Java methods: 1 JS function does not need to specify the return type, nor does it need to write the type of formal parameter, but only the name of formal parameter

2. When calling a function, the number of arguments can be inconsistent with the formal parameters

Function call: function name (argument 1, argument 2.. argument n)

3. Anonymous function: function without name

Function expression: assign an anonymous function to a variable, and the variable is the function expression

Anonymous functions can be called using function expressions

4. Arguments: used in the function body, it is an array that encapsulates all arguments

5. Local variable: the variable declared with VaR in the function, which cannot be accessed outside the function

6. Global variables: variables declared with VaR outside the function or variables not declared with VaR inside the function are called global variables

Note: all global variables belong to window objects. All global variables are destroyed only when the window is closed. Generally, window is not required, JS has only function scope (global scope or local scope) and no block scope

2.1 timer:

2.1. 1 cycle timer: setinterval(), which periodically executes the specified function or JS code

Parameter 1: function or code string to call

Parameter 2: the time interval of periodic execution. The unit is milliseconds. The function or code is executed every milliseconds

There are two kinds of calling syntax for calling a function repeatedly:

Syntax 1: setinterval (function name, time interval); It is only suitable for calling functions without parameters

Syntax 2: setinterval ("function name ()", time interval); You can call nonparametric functions or functions with parameters

2.1. 2 bomb timer: settimeout(), execute the specified function or JS code once after reaching the specified time

Parameter 1: function or code string to call

Parameter 2: execution interval, in milliseconds. Function or code is executed every milliseconds

Call a function once. There are two kinds of call syntax:

Syntax 1: setTimeout (function name, time interval); It is only suitable for calling functions without parameters

Syntax 2: setTimeout ("function name ()", time interval); You can call nonparametric functions or functions with parameters

2.1. 3 clear timer: sometimes it is necessary to stop the repeated execution of the timer at a certain time. How to end it?

1. Add a return value to the timer function, which is the unique identification of the timer

2. When it is necessary to end the timer, call clearinterval (unique identifier of the timer) to end the cycle timer,

Cleartimeout() ends the bomb timer

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>