Reading Notes of javascript advanced programming (4) reference type and reference type
Chapter 5: reference types
Object Type
You can create an object instance in either of the following ways:
1. new Method
Var person = new Object ();
Person. name = "haozk ";
Person. age = 24;
2. Representation of object literal
Var person = {
Name: "haozk ",
Age: 24
}
You can also enclose attributes with strings:
Var person = {
"Name": "haozk ",
"Age": 24
}
There are two ways to access the attributes of an object instance:
1. alert (person ["name"]) // square brackets (attributes can be accessed through variables or keywords or reserved words)
2. alert (person. name) // Point Method
Array type
Unlike arrays in other programming languages, arrays in js have dynamic resizing of stringbuffer, and data item types can be different.
There are two ways to create, new and array literal:
Var colors = new Array ();
Var colors = new Array (3 );
Var colors = new Array ("red", "blue", "green ");
Var colors = [];
Var colors = ["red", "blue", "green"];
The length attribute of the array is not read-only and can be changed:
Colors. length = 2;
Alert (colors [2]); // undefined
Colors [colors. length] = 'black'; // Add a color in position 2. The current length is 3.
Detection array: (important)
Array. isArray () method
If (Array. isArray (value )){
// Operations on Arrays
}
Conversion Method:
To call the toLocaleString (), toString (), and valueOf () of the array, the returned results are strings separated by commas.
Alert () accepts string parameters and calls the toString () method in the background. Therefore, you can directly use alert (colors ).
To call the join () method, you can accept the parameter as a separator string, for example:
Alert (colors. join ["|"]) // red | blue | green
Stack method: push (), pop ()
Queue method: push (), shift (), unshift (can add any item at the front end of the array and return the length of the new array)
Re-sorting method:
Reverse: reverse ()
Ascending: sort () (Note: sort by string instead of numeric value)
Function compare (value1, value2) {// ascending (General)
If (value1 <value2 ){
Return-1; // in descending order, return 1;
} Else if (value1> value ){
Return 1; // in descending order, return-1;
} Else {
Return 0;
}
}
Var values = [0, 1, 5, 10, 15];
Values. sort (compare );
Alert (values );
Operation Method: contact (), slice (), splice ()
Location Method: indexOf (), lastIndexOf ()
Iteration Methods: every (), filter (), forEach (), map (), some ()
Reduction Method: reduce (), reduceRight ()