This article summarizes the three attributes of the Array array, the length attribute, the prototype attribute, the constructor attribute, and the 8 classifications and multiple methods of the array object. The specifics are as follows:
3 attributes of the object
1. Length attribute
length attribute
Length attribute represents the length of the array, the number of elements. Because the index of the array always starts from 0, the upper and lower limits of an array are: 0 and length-1. Different from most of the other, the Length attribute of the JavaScript array is variable, which requires special attention. When the length attribute is set larger, the status of the entire array will not change, just the length attribute becomes larger; when the length attribute is set than the original hour, the original array indexes are greater than or equal to the element of length. All the values are lost. The following is an example of demonstrating the length attribute:
VAR ARR = [12,23,5,3,25,98,76,54,56,56,76]; // Define one array containing 10 numbers
Alert (Arr.Length); // Display the length of the array 10
Arr.Length = 12; // Increases the length of the array
Alert (Arr.Length); // The length of the array has become 12
Alert (ARR [8]); // Display the value of the 9th element, 56
Arrr.Length = 5; // reduce the length of the array to 5, and the index equal to or more than 5 elements are discarded
Alert (ARR [8]); // Show the ninth element has become "undefined"
Arr.Length = 10; // Restore the array length to 10
Alert (ARR [8]); Although the length is restored to 10, the 9th element cannot be recovered, showing "undefined"
From the above code, we can clearly see the nature of the length attribute. But the Length object can not only be set up, but also may be modified in hidden. You can use an unlead variable in JavaScript. Similarly, you can also use an unfarished array element (referring to the element that indexes exceeds or is equal to length). Add 1. For example, the following code:
VAR ARR = [12,23,5,3,25,98,76,54,56,56,76]; // Define one array containing 10 numbers
Alert (Arr.Length); // Display 10
arr [15] = 34;
Alert (Arr.Length); // Show 16
The code also defines an array containing 10 numbers first. It can be seen through the Alert statement that its length is 10. Subsequently, the index is 15 elements, which is assigned to 15, that is, ARR [15] = 34. At this time, the length of the array output array with the alert statement is 16. In any case, this is a very surprising feature for developers who are used to strong types of programming. In fact, the initial length created by the new array () form is 0, which is the operation of the unspecific element in which the length of the array is changed.
As can be seen from the above introduction, the length attribute is so amazing, using it to easily increase or reduce the capacity of the array. Therefore, the in -depth understanding of the length attribute will help to use it flexibly during the development process.
2. Prototype attribute
Prototype attribute
Returns the reference to the object type prototype. Prototype attributes are shared by Object.
objectName.prototype
ObjectName parameter is the name of Object object.
Note: Use the Prototype property to provide a set of basic functions of the class of the object. The new instance of the object "inheritance" gives the prototype of the object.
For array objects, the following examples illustrate the use of the prototype attribute.
Add the maximum elemental value to the array object. To complete this, declare a function, add it to Array.prototype and use it.
Function array_max () ()
{{
var I, max = this [0];
for (i = 1; I <THIS.Length; I ++)
{{
if (MAX <THIS [i])
max = this [i];
}
Return max;
}
Array.prototype.max = array_max;
var x = new array (1, 2, 3, 4, 5, 5, 6);
var y y = x.max ();
After the code is executed, the maximum value of the array X, or 6.
3. Constructionor attribute
constructor attribute
Indicates the function of creating objects.
object.constructor // object is the name of an object or function.
Note: Constructor attributes are members of all objects with prototype. They include all JScript objects except the Global and Math objects. Constructor property preserves a reference to a function that constructs a specific object instance.
For example:
x = New String ("Hi");
If (x.conStructor == String) // For processing (the condition is true).
//or
Function myfunc {
// Function body.
}
y = new myfunc;
if (y.conStructor == MyFunc) // For processing (the condition is true).
For the array:
y = new array ();
8 categories and multiple methods of array objects
1. Array creation
var arrayObj = new array (); // Create a default array, 0 in length
var arrayObj = New Array (size); // Create an array of size length, note that the length of Array is variable, so it is not the upper limit, it is the length, it is the length, it is the length, it is the length, it is the length, but the length
var arrayObj = New Array (item1, item2,); // Create an array and give an initial value
It should be explained that although the second method creates the length of the array, in fact, the array is longer in all cases, that is, even if the length is specified, it can still store the element outside the specified length. Note: At this time, the length will change.
2. Access for elements of array
var arrayItemValue = ArrayObj [1]; // Get the elemental value of the array
arrayObj [1] = "Give new value"; // Gives new values to array elements
This article summarizes the three attributes of the Array array, the length attribute, the prototype attribute, the constructor attribute, and the 8 classifications and multiple methods of the array object. The specifics are as follows:
3 attributes of the object
1. Length attribute
length attribute
Length attribute represents the length of the array, the number of elements. Because the index of the array always starts from 0, the upper and lower limits of an array are: 0 and length-1. Unlike most other languages, the length attribute of the JavaScript array is variable, which requires special attention. When the length attribute is set larger, the status of the entire array will not change, just the length attribute becomes larger; when the length attribute is set than the original hour, the original array indexes are greater than or equal to the element of length. All the values are lost. The following is an example of demonstrating the length attribute:
VAR ARR = [12,23,5,3,25,98,76,54,56,56,76]; // Define one array containing 10 numbers
Alert (Arr.Length); // Display the length of the array 10
Arr.Length = 12; // Increases the length of the array
Alert (Arr.Length); // The length of the array has become 12
Alert (ARR [8]); // Display the value of the 9th element, 56
Arrr.Length = 5; // reduce the length of the array to 5, and the index equal to or more than 5 elements are discarded
Alert (ARR [8]); // Show the ninth element has become "undefined"
Arr.Length = 10; // Restore the array length to 10
Alert (ARR [8]); Although the length is restored to 10, the 9th element cannot be recovered, showing "undefined"
From the above code, we can clearly see the nature of the length attribute. But the Length object can not only be set up, but also may be modified in hidden. You can use an unlead variable in JavaScript. Similarly, you can also use an unfarished array element (referring to the element that indexes exceeds or is equal to length). Add 1. For example, the following code:
VAR ARR = [12,23,5,3,25,98,76,54,56,56,76]; // Define one array containing 10 numbers
Alert (Arr.Length); // Display 10
arr [15] = 34;
Alert (Arr.Length); // Show 16
The code also defines an array containing 10 numbers first. It can be seen through the Alert statement that its length is 10. Subsequently, the index is 15 elements, which is assigned to 15, that is, ARR [15] = 34. At this time, the length of the array output array with the alert statement is 16. In any case, this is a very surprising feature for developers who are used to strong types of programming. In fact, the initial length created by the new array () form is 0, which is the operation of the unspecific element in which the length of the array is changed.
As can be seen from the above introduction, the length attribute is so amazing, using it to easily increase or reduce the capacity of the array. Therefore, the in -depth understanding of the length attribute will help to use it flexibly during the development process.
2. Prototype attribute
Prototype attribute
Returns the reference to the object type prototype. Prototype attributes are shared by Object.
objectName.prototype
ObjectName parameter is the name of Object object.
Note: Use the Prototype property to provide a set of basic functions of the class of the object. The new instance of the object "inheritance" gives the prototype of the object.
For array objects, the following examples illustrate the use of the prototype attribute.
Add the maximum elemental value to the array object. To complete this, declare a function, add it to Array.prototype and use it.
Function array_max () ()
{{
var I, max = this [0];
for (i = 1; I <THIS.Length; I ++)
{{
if (MAX <THIS [i])
max = this [i];
}
Return max;
}
Array.prototype.max = array_max;
var x = new array (1, 2, 3, 4, 5, 5, 6);
var y y = x.max ();
After the code is executed, the maximum value of the array X, or 6.
3. Constructionor attribute
constructor attribute
Indicates the function of creating objects.
object.constructor // object is the name of an object or function.
Note: Constructor attributes are members of all objects with prototype. They include all JScript objects except the Global and Math objects. Constructor property preserves a reference to a function that constructs a specific object instance.
For example:
x = New String ("Hi");
If (x.conStructor == String) // For processing (the condition is true).
//or
Function myfunc {
// Function body.
}
y = new myfunc;
if (y.conStructor == MyFunc) // For processing (the condition is true).
For the array:
y = new array ();
8 categories and multiple methods of array objects
1. Array creation
var arrayObj = new array (); // Create a default array, 0 in length
var arrayObj = New Array (size); // Create an array of size length, note that the length of Array is variable, so it is not the upper limit, it is the length, it is the length, it is the length, it is the length, it is the length, but the length
var arrayObj = New Array (item1, item2,); // Create an array and give an initial value
It should be explained that although the second method creates the length of the array, in fact, the array is longer in all cases, that is, even if the length is specified, it can still store the element outside the specified length. Note: At this time, the length will change.
2. Access for elements of array
var arrayItemValue = ArrayObj [1]; // Get the elemental value of the array
arrayObj [1] = "Give new value"; // Gives new values to array elements