JavaScript Core Objects
- Array array object
- String object
- Date object
- Math Object
Array objects
Array objects are used to store a series of values in a single variable name. Array is a data structure commonly used in programming languages. It can be used to store a series of values. In JavaScript, different data types can be stored in the same array.
1. Create arrays
- Create an array of size equal
var array = new Array();
- Create an array of size size
var array = new Array(size);
- Create an array and assign values
var array = new Array(elements0,element1,......);
2. Access arrays
Array elements can be accessed through an index with an index value of 0 ~ (length-1), such as:
var array = new Array(2,3,5);
dcoument.write(array[1]); the result of // output is 3
3. Attributes of arrays
- The length attribute
The length attribute represents the length of the array, that is, the number of elements contained in the array.
For example:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>length</title> <script language="JavaScript"> var array = new Array("A","B","C","D"); document.write("The initial array elements are:"); printarray(array); array.length = 7; document.write("length The enlarged array elements are:"); printarray(array); array.length = 3; document.write("length The reduced array elements are:"); printarray(array); document.write("arry[5]="+arry[5]); function printarray(array) { for (var i = 0;i<array.length;i++){ document.write(array[i]+" "); } document.write("<br>"); } </script> </head> <body> </body> </html>
- prototype attribute
prototype attributes are attributes common to all JavaScript objects that are used to add newly defined attributes or methods to objects, and instances of objects can call added methods or attributes.
Grammar:
Array.prototype.methodName = functionName;
Or
Array.prototype.methodName = functionName([param1],[param2]…){
statem;
}
For example:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>length</title> <script language="JavaScript"> function array_max() { var i,max=this[0]; for(i=1;i<this.length;i++){ if(max<this[i]){ max=this[i]; } } return max; } function array_print() { for (var i=0;i<this.length;i++){ document.write(this[i]+" "); } document.write("<br>"); } Array.prototype.max = array_max; Array.prototype.print = array_print; var array = new Array(32,8,-12,156,78); document.write("The value of the array is:"); array.print(); document.write("The largest element value in the array is:"+array.max()); </script> </head> <body> </body> </html>
- constructor attribute
The constructor attribute is used to return a reference to the array function that created the object.
For example:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>constructor</title> <script> var a = new Array(); if (a.constructor = array){ document.write("array is Array"); } else if (a.constructor = Boolean){ document.write("a is Boolean"); } </script> </head> <body> </body> </html>
4. Array Method
Method | describe |
concat() | Connect two or more arrays and return the results. |
join() | Put all elements of an array into a string. Elements are delimited by a specified delimiter. |
pop() | Delete and return the last element of the array |
push() | Add one or more elements to the end of the array and return a new length. |
reverse() | Reverse the order of elements in an array. |
shift() | Delete and return the first element of the array |
slice() | Returns the selected element from an existing array |
sort() | Sort the elements of an array |
splice() | Delete elements and add new elements to the array. |
toSource() | Returns the source code for the object. |
toString() | Converts the array to a string and returns the result. |
toLocaleString() | Convert the array to a local array and return the result. |
unshift() | Add one or more elements to the beginning of the array and return a new length. |
valueOf() | Returns the original value of an array object |
I hereby declare that if you need to reproduce, please indicate the source, if you have any questions, please put forward in time for correction, if there is any infringement, contact to delete, thank you.