Array Objects |
Active Web |
Array objects give special treatment to a certain class of property names. A property name is an array index if it is a number in the range 0 to 2 to the power of 32.
Every Array object has a length property whose value is always a non negative integer. The value of the length property is numerically greater than the name of every property whose name is an array index; whenever a property of an Array object is created or changed, other properties are adjusted as necessary to maintain this invariant. Specifically, whenever a property is added whose name is an array index, the length property is changed, if necessary, to be one more than the numeric value of that array index; and whenever the length property is changed, every property whose name is an array index whose value is not smaller than the new length is automatically deleted. This constraint applies only to properties of the Array object itself and is unaffected by length or array index properties that may be inherited from its prototype.
Array objects are of class Array.
Property |
Description |
isArray |
This is a boolean set to true. This is used to identify this object as an array. All other objects will return this property as false. |
length |
The length property is always numerically greater than the name of every property whose name is an array index. Changing this property will remove all indexed properties whose name is less than the new length. Note: Not all indexed properties whose name is less than the length have to have a value. |
prototype |
A reference to the Array Prototype. This property does not enumerate and can not be deleted but can be changed. |
The Array object has no predefined functions.
The Array constructor is used to create new array objects. All array objects are given the Array prototype.
var myArray1 = new Array();
A new Array object with no properties and a length of zero is created.
var myArray2 = new Array(len);
If the len argument is a number primitive or a number object then a new Array object is created with a length of len. If the argument is not a number then a new Array object is created with a length of 1 and the 0 property is set to the len argument.
var myArray3 = new Array(item0, item1 [, ...]);
This description only applies if there are two or more arguments. A new Array object is created with the length set to the number of arguments and the arguments set as properties 0 to n (where n is the number of arguments -1) in the order in which they have been supplied (e.g. item0 is property 0).
Shown below is the Array object prototype chain.
When the constructor is run as a function without the new operator it has exactly the same behaviour as using the new operator.
Constructor properties are read only (they can not be changed or deleted) and it is not possible to add new properties to the Constructor.
Property |
Description |
prototype |
A reference to the Array Prototype. This property does not enumerate and can not be changed or deleted. |
The Array constructor has no predefined functions.
This prototype can be used by any object that has properties with index names.
Property |
Description |
prototype |
A reference to the Object Prototype. This property does not enumerate and can not be deleted but can be changed. |
The this reference used here always refers to the object that was used to run the function.
Except where explicitly stated otherwise all these functions are intentionally generic. They do not require that the this value or the arguments be Array objects, and can therefore be applied to other objects where they will attempt to use indexed properties.
Function |
Description |
concat([item1[, item2[, . . .]]]) |
When the concat function is called with zero or more arguments item1, item2, etc., it returns a new array containing the array elements of this object followed by the array elements of each argument in order. This function is intentionally generic and can be applied to other objects, but in this case the new array will have the toString function of this object as the first element off the new array (i.e. it will not attempt to use indexed properties if this is not an array object). |
join(separator) |
Returns a string representation of the array. The elements of the array are converted to strings, and these strings are then concatenated, separated by occurrences of the separator. If no separator is provided, a single comma is used as the separator. |
pop() |
The last element of the array is removed from the array and returned. |
push([item1[, item2[, . . .]]]) |
The arguments are appended to the end of the array, in the order in which they appear. The new length of the array is returned as the result of the call. |
reverse() |
The elements of the array are rearranged so as to reverse their order. This object is returned as the result of the call. |
shift() |
The first element of the array is removed from the array and returned. |
slice(start, end) |
The slice method takes two arguments, start and end, and returns an array containing the elements of the array from element start up to, but not including, element end (or through the end of the array if end is undefined). If start is negative, it is treated as (length+start) where length is the length of the array. If end is negative, it is treated as (length+end) where length is the length of the array. |
sort(function[, args[, . . .]]) |
Returns a new array sorted in alphabetical order. The array elements are converted to strings using their toString function and an alphabetic sort is done on the elements. The callback function is optional and if not present then a standard alphabetic sort is used. If the function is specified then it is used to define the sort. The sort goes through the array and calls the function with two adjacent elements. The function should return true indicating that the position of the elements should be swapped or false if they should remain unchanged. This is repeated until no more element swapping takes place. Any extra arguments specified in this call are passed on to the sort function each time it is called. |
splice(start, deleteCount[, item1[, item2[, . . .]]]) |
When the splice method is called with two or more arguments start, deleteCount and (optionally) item1, item2, etc., the deleteCount elements of the array starting at array index start are replaced by the arguments item1, item2, etc. |
toLocaleString() |
Returns the same as toString. This function is here so that in the future we can provide a Locale dependant version of toString. |
toString() |
Returns a string representation of the array. The elements of the array are converted to strings using their toString functions, and these strings are then concatenated, separated by commas. The result of calling this function is the same as if the built-in join function were invoked for this object with no argument. This function is not generic. If the this object is not an array object then it returns a string of the form "[object className]" where className is the class of this object. |
unshift([item1[, item2[, . . .]]]) |
The arguments are prepended to the start of the array, such that their order within the array is the same as the order in which they appear in the argument list. |
The following example builds an array and then sorts it using a callback function. The array consists of compound objects and the sort function uses the height property or the name property in each object to make its decision on the sort order.
example:
function sortByHeight(element1, element2) {
if (element1.height element2.height) return true;
return false;
}
function sortByName(element1, element2) {
if (element1.compareTo(element2) 0) return true;
return false;
}
var personA = "Bill";
personA.height = 2.1;
var personB = "Susan";
personB.height = 1.7;
var personC = "Harry";
personC.height = 1.9;
var array = new Array(personA, personB, personC);
var arrayA = array.sort(sortByHeight);
var arrayB = array.sort(sortByName);
The elements in arrayA will be: personB, personC, personA. The elements in arrayB will be: personA, personC, personB.
Topic ID: 150033