Definition of reference types:
The value (object) of the reference type is an instance of the reference type.
In ECMAScript, the reference type is a data structure used to organize data and functions together. It is also called a class, but in js, the class name is not appropriate.
In terms of technology, JavaScript is an object-oriented language, but strictly speaking, JavaScript does not have basic attributes such as classes, interfaces, and inheritance supported by object-oriented languages.
The reference type is also called [object definition] because it describes the attributes and methods of a class of objects.
Section 1: Object Type
Objects can be said to be the most commonly used type in Javascript. Although the Object instance does not have many functions, it is an ideal choice for storing and transmitting data in programs.
Create an Object:
(1) create with the new operator:
1
Var Obj = new Object ();
2
Obj. a = "";
3
Obj. B = function (){
4
//.........
5
}
(2) create with object literal: This method has the feeling of encapsulation, and is very suitable for transmitting a large number of parameters
1
Var Obj = {
2
A: "",
3
B: function (){
4
//............
5
}
6
}
(3) If the curly braces {} are left blank during creation, you can define an object that contains only its default attributes and methods.
1
Var Obj = {};
2
Obj. a = "";
3
//......
Note: Object constructor is not called when an Object is defined using a literal (except for firefox)
Two methods to access object attributes:
(1) access through the. OPERATOR: Obj.;
(2) access through []: Obj [a];
There is no difference between the two methods. However, the advantage of access through [] Is that variables can be used, for example:
1
Var Obj = {
2
A: "",
3
B: "B ",
4
}
5
Var c = "";
6
Alert (Obj [c]) //
Section 2: Array type
1. Create an Array object using Constructors
(1) Use Array constructor: var a = new Array ();
(2) If you know the number of items in the Array in advance, you can: var a = new Array (20), but the values of these 20 items are Undefined.
(3) You can also directly pass the required item to the constructor: var a = new Array ("red", "blue ");
(4) pass a value directly to the constructor to create an empty project with the number of values: var a = new Array (3)
(5) When creating an Array, you can also omit the new operator: var a = Array ("red", "blue ");
2. Create an Array object literally
1
Var a = ["red", "blue", "black"]; // OK
2
Var B = []; // creates an empty array.
3
Var d = ["red", "blue",] // This is not the case. Different browsers cannot explain this. Two or three items may be created.
4
Var d = [,] // This is not the case. Different browsers cannot explain this. Five or six items may be created.
3. The length attribute of the array
The length attribute of the array is not read-only. Therefore, you can set the length to add or remove items from the end of the array:
01
Var a = ["red", "blue", "black"];
02
A. length = 2; // Delete the End Project
03
Alert (a [2]) // undefined;
04
05
Var B = ["red", "blue", "black"];
06
B. length = 4; // Add a new project at the end. The default value is undefined.
07
Blert (a [4]) // undefined;
08
09
Var c = ["red", "blue", "black"];
10
C [c. length] = "orange"; // insert a project directly at the end of the array with the value orange
11
C [10] = "white"; // insert a project in array position 10. If the value is white, the project at position 4-9 is undefined by default.
4. Conversion Method
All objects have the toString (), toLocaleString (), and valueOf () methods.
The same value is returned by calling the toString () and valueOf () Methods of the array. This will call the toString () method of each item of the array in the background to splice the final string.
1
Var a = ["red", "blue", "black"];
2
Alert (a. toString () // output: red, blue, black;
3
Alert (a. valueOf () // output: red, blue, black;
Join method: When the array's toString () and other methods are output, they are separated by default. You can use join to specify the delimiter.
1
Var a = ["red", "blue", "green"];
2
Alert (a. join ("/"); // output red/blue/green
5. Stack and queue Methods
(1) push () receives any number of parameters, adds them to the end of the array, and returns the length of the new array.
(2) pop () removes a project from the end of the array and returns the project. It can be used together with push () to simulate stack operations;
(3) shift () can remove the first item of the array and return to the project. Together with push (), It can simulate queue operations.
(4) unshift () receives any number of parameters, adds a project to the front-end of the array, and returns the length of the new array.
6. Re-sorting method
(1) reverse () reverses the order of array items
(2) by default, sort () sorts the array items in ascending order, that is, the smallest one is at the beginning. to sort the items, sort () calls the toString () method of each item to convert them into strings, then compare the string to be sorted.
1
// Reverse ()
2
Var a = [1, 2, 3, 4, 5];
3
Var B = a. reverse ();
4
Alert (B); // 5, 4, 3, 2, 1
5
6
// Sort ()
7
Var a = [0, 1, 5, 10, 15]
8
Var B = a. sort ();
9
Alert (B) // 0, 1, 10, 15, 5
It can be seen that the default sorting of sort () is not intelligent. Fortunately, sort () can receive a comparison function as a parameter to implement custom sorting.
01
Function compare (x, y ){
02
If (x <y) {// if the first parameter is before the second parameter,-1 is returned;
03
Return-1;
04
} Else if (x> y) {// if the first parameter is placed after the second parameter, 1 is returned;
05
Return 1;
06
} Else {// if the two are equal, return 0;
07
Return 0;
08
}
09
// The above judgment code can be replaced with a simpler return y-x;
10
} // Define a comparison function that receives connection Parameters
11
12
Var a = [0, 1, 5, 10, 15];
13
A. sort (compare );
14
Alert ();//
Both reverse () and sort () return the modified array.
7. Operation Method
(1) concat () creates a new array based on the current array and adds each received value or array to the new array.
1
Var a = ["red", "blue"];
2
Var B = a. concat ("green", "black ");
3
Alert (B) // red, blue, green, black
4
5
Var c = a. concat (["green", "black"]) // The accepted parameter is an array.
6
Alert (c) // red, blue, green, black
(2) slice () can create an array based on the current number group and receive one or two parameters, that is, the start and end positions of the project to be returned.
1
Var a = ["red", "blue", "green", "black"];
2
Var B = a. slice (0, 2 );
3
Alert (B); // red, blue, green
4
5
Var c = a. slice (2 );
6
Alert (c); // green, black
If the slice () parameter has a negative number, the array Length plus a negative number is used to determine its position. If the start position is greater than the end position, an empty array is returned.
If it is a negative number, it can be understood as starting from the end of the array, the starting position is changed to the ending position, and the whole is reversed ..
(3) splice () is the most powerful array operation method. It is mainly used to insert a project into the middle of the array. There are three methods:
Delete -- any item can be deleted. You only need to specify two parameters: the first position of the item to be deleted and the number of items to be deleted;
Insert -- insert any number of items at the specified position. At least three parameters are provided: Start position and 0 (the number of items to be deleted is 0, indicating the insert operation), the items to be inserted (multiple items can be inserted)
Replace -- delete any number of projects at the specified position and insert any number of projects. The deletion and insertion can be different. Specify at least three parameters: Start position, number of projects to be deleted, and project to be inserted.
1
Var a = ["red", "blue", "green", "black"];
2
Var B = a. splice (0, 1 );
3
Var c = a. splice (1, 0, "white", "orange ");
4
Var d = a. splice (2, 2, "white", "orange ")
5
6
Alert (B); // Delete the first item blue, green, black
7
Alert (c); // insert two items red, blue, white, orange, green, black
8
Alert (d); // replace two items, red, blue, green, white, and orange.
Splice () always returns an array that contains the items to be deleted. If the items are not deleted, an empty array is returned.
Section 3: Date type
Author: the soul of freedom
Definition of reference types:
The value (object) of the reference type is an instance of the reference type.
In ECMAScript, the reference type is a data structure used to organize data and functions together. It is also called a class, but in js, the class name is not appropriate.
In terms of technology, JavaScript is an object-oriented language, but strictly speaking, JavaScript does not have basic attributes such as classes, interfaces, and inheritance supported by object-oriented languages.
The reference type is also called [object definition] because it describes the attributes and methods of a class of objects.
Section 1: Object Type
Objects can be said to be the most commonly used type in Javascript. Although the Object instance does not have many functions, it is an ideal choice for storing and transmitting data in programs.
Create an Object:
(1) create with the new operator:
1
Var Obj = new Object ();
2
Obj. a = "";
3
Obj. B = function (){
4
//.........
5
}
(2) create with object literal: This method has the feeling of encapsulation, and is very suitable for transmitting a large number of parameters
1
Var Obj = {
2
A: "",
3
B: function (){
4
//............
5
}
6
}
(3) If the curly braces {} are left blank during creation, you can define an object that contains only its default attributes and methods.
1
Var Obj = {};
2
Obj. a = "";
3
//......
Note: Object constructor is not called when an Object is defined using a literal (except for firefox)
Two methods to access object attributes:
(1) access through the. OPERATOR: Obj.;
(2) access through []: Obj [a];
There is no difference between the two methods. However, the advantage of access through [] Is that variables can be used, for example:
1
Var Obj = {
2
A: "",
3
B: "B ",
4
}
5
Var c = "";
6
Alert (Obj [c]) //
Section 2: Array type
1. Create an Array object using Constructors
(1) Use Array constructor: var a = new Array ();
(2) If you know the number of items in the Array in advance, you can: var a = new Array (20), but the values of these 20 items are Undefined.
(3) You can also directly pass the required item to the constructor: var a = new Array ("red", "blue ");
(4) pass a value directly to the constructor to create an empty project with the number of values: var a = new Array (3)
(5) When creating an Array, you can also omit the new operator: var a = Array ("red", "blue ");
2. Create an Array object literally
1
Var a = ["red", "blue", "black"]; // OK
2
Var B = []; // creates an empty array.
3
Var d = ["red", "blue",] // This is not the case. Different browsers cannot explain this. Two or three items may be created.
4
Var d = [,] // This is not the case. Different browsers cannot explain this. Five or six items may be created.
3. The length attribute of the array
The length attribute of the array is not read-only. Therefore, you can set the length to add or remove items from the end of the array:
01
Var a = ["red", "blue", "black"];
02
A. length = 2; // Delete the End Project
03
Alert (a [2]) // undefined;
04
05
Var B = ["red", "blue", "black"];
06
B. length = 4; // Add a new project at the end. The default value is undefined.
07
Blert (a [4]) // undefined;
08
09
Var c = ["red", "blue", "black"];
10
C [c. length] = "orange"; // insert a project directly at the end of the array with the value orange
11
C [10] = "white"; // insert a project in array position 10. If the value is white, the project at position 4-9 is undefined by default.
4. Conversion Method
All objects have the toString (), toLocaleString (), and valueOf () methods.
The same value is returned by calling the toString () and valueOf () Methods of the array. This will call the toString () method of each item of the array in the background to splice the final string.
1
Var a = ["red", "blue", "black"];
2
Alert (a. toString () // output: red, blue, black;
3
Alert (a. valueOf () // output: red, blue, black;
Join method: When the array's toString () and other methods are output, they are separated by default. You can use join to specify the delimiter.
1
Var a = ["red", "blue", "green"];
2
Alert (a. join ("/"); // output red/blue/green
5. Stack and queue Methods
(1) push () receives any number of parameters, adds them to the end of the array, and returns the length of the new array.
(2) pop () removes a project from the end of the array and returns the project. It can be used together with push () to simulate stack operations;
(3) shift () can remove the first item of the array and return to the project. Together with push (), It can simulate queue operations.
(4) unshift () receives any number of parameters, adds a project to the front-end of the array, and returns the length of the new array.
6. Re-sorting method
(1) reverse () reverses the order of array items
(2) by default, sort () sorts the array items in ascending order, that is, the smallest one is at the beginning. to sort the items, sort () calls the toString () method of each item to convert them into strings, then compare the string to be sorted.
1
// Reverse ()
2
Var a = [1, 2, 3, 4, 5];
3
Var B = a. reverse ();
4
Alert (B); // 5, 4, 3, 2, 1
5
6
// Sort ()
7
Var a = [0, 1, 5, 10, 15]
8
Var B = a. sort ();
9
Alert (B) // 0, 1, 10, 15, 5
It can be seen that the default sorting of sort () is not intelligent. Fortunately, sort () can receive a comparison function as a parameter to implement custom sorting.
01
Function compare (x, y ){
02
If (x <y) {// if the first parameter is before the second parameter,-1 is returned;
03
Return-1;
04
} Else if (x> y) {// if the first parameter is placed after the second parameter, 1 is returned;
05
Return 1;
06
} Else {// if the two are equal, return 0;
07
Return 0;
08
}
09
// The above judgment code can be replaced with a simpler return y-x;
10
} // Define a comparison function that receives connection Parameters
11
12
Var a = [0, 1, 5, 10, 15];
13
A. sort (compare );
14
Alert ();//
Both reverse () and sort () return the modified array.
7. Operation Method
(1) concat () creates a new array based on the current array and adds each received value or array to the new array.
1
Var a = ["red", "blue"];
2
Var B = a. concat ("green", "black ");
3
Alert (B) // red, blue, green, black
4
5
Var c = a. concat (["green", "black"]) // The accepted parameter is an array.
6
Alert (c) // red, blue, green, black
(2) slice () can create an array based on the current number group and receive one or two parameters, that is, the start and end positions of the project to be returned.
1
Var a = ["red", "blue", "green", "black"];
2
Var B = a. slice (0, 2 );
3
Alert (B); // red, blue, green
4
5
Var c = a. slice (2 );
6
Alert (c); // green, black
If the slice () parameter has a negative number, the array Length plus a negative number is used to determine its position. If the start position is greater than the end position, an empty array is returned.
If it is a negative number, it can be understood as starting from the end of the array, the starting position is changed to the ending position, and the whole is reversed ..
(3) splice () is the most powerful array operation method. It is mainly used to insert a project into the middle of the array. There are three methods:
Delete -- any item can be deleted. You only need to specify two parameters: the first position of the item to be deleted and the number of items to be deleted;
Insert -- insert any number of items at the specified position. At least three parameters are provided: Start position and 0 (the number of items to be deleted is 0, indicating the insert operation), the items to be inserted (multiple items can be inserted)
Replace -- delete any number of projects at the specified position and insert any number of projects. The deletion and insertion can be different. Specify at least three parameters: Start position, number of projects to be deleted, and project to be inserted.
1
Var a = ["red", "blue", "green", "black"];
2
Var B = a. splice (0, 1 );
3
Var c = a. splice (1, 0, "white", "orange ");
4
Var d = a. splice (2, 2, "white", "orange ")
5
6
Alert (B); // Delete the first item blue, green, black
7
Alert (c); // insert two items red, blue, white, orange, green, black
8
Alert (d); // replace two items, red, blue, green, white, and orange.
Splice () always returns an array that contains the items to be deleted. If the items are not deleted, an empty array is returned.
Section 3: Date type