|
After you create an array you can increase its length by specifying a value for the highest subscript element. The following code creates an array of length zero, then assigns a null value to element 99. This changes the length of the array to 100.
accounts = new Array() // array of zero elements accounts[99] = null // array of 100 elements
Note that the array does not take up any storage space, even after it is extended. When referring to an element, the subscript can be either a literal (e.g., 3) or a variable (e.g., num = 3). An element of an array can be any valid value. It can be a string, a number, a Boolean value, a null value, or even another object. For example, if you want to create an array in which each element is a student object, you can use the following statements:
function student() { // constructor function // properties not initialized to meaningful value this.name = "" this.age = "" this.grade = "" } var size = 35 // num of students in class var students = new Array(size) // array is defined for (var i = 0; i < size; i++) { students[i] = new student() } students[0].name = "Mark" students[32].grade = 88
At first, the desired size of the array, the number of students in the class, is assigned to the variable size. An array of that size is then created. All elements of the array, from students[0] to students[34], are then defined using the constructor function student(). In this example, all of the elements in the array are of the same type. An array can have elements of different types. Here is an example:
function student() { // constructor function // properties not initialized to meaningful value this.name = "" this.age = "" this.grade = "" } function teacher(name, age) { this.name = name this.age = age } var size = 35 // num of students in class var students = new Array(size + 1) // array is defined students[0] = new teacher("Kate", 45) for (var i = 1; i < size + 1; i++) { // or i <= size students[i] = new student() } alert("is the teacher." + students[0].name)
In this script segment an array of size + 1 elements is defined, because the first element, students[0], holds an instance of the teacher object. The output of this script is: The most important rule is that the subscript, or index, starts at zero. Although it might seem quite awkward, use this element like all other elements of the array. It looks better if you start referencing at 0 rather than at 1, like some amateur scripters do. Creating Dense ArraysYou can construct a dense array of two or more elements starting with index (subscript) 0, if you define initial values for all elements. A dense array is one in which each element has a value. They are very popular in many scripting languages, especially in Perl. You can populate an array by specifying the values of its elements:
var bb = "baseball" var sports = new Array("football", bb, "basketball", "soccer")
You can refer to the elements of this array with the common syntax:
sports[0] == "football" sports[1] == "baseball" var val = 2 sports[val] == "basketball" sports[3] == "soccer"
JavaScript for Navigator 3.0 still lacks many features related to arrays that are supported by Perl:
Array TypesJavaScript is a loosely typed language, just like Perl. It is not surprising, therefore, that elements of an array can be of different types. Some elements of a given array can be strings, other can be numbers, Boolean values, and even objects. Basically, there are five types of arrays:
Sometimes you want to know what type of array you are dealing with. JavaScript does not include any tool to facilitate this. However, using a prototype we can add a property to all arrays (remember, arrays are objects by which you can create instances) which will return the type of the array. Here is the desired method:
function getType() { var arrayType = typeof this[0] for (var i = 1; i < this.length; ++i) { if (typeof this[i] != arrayType) { arrayType = "mixed" break } } return arrayType } Array.prototype.getType = getType
The following script segment is based on the preceding prototype definition:
var ar1 = new Array(3) ar1[0] = "a" ar1[1] = "b" ar1[2] = "" document.write(ar1.getType()) // string var ar2 = new Array(2) ar2[0] = 17 ar2[1] = 15.5 document.write(ar2.getType()) // number var ar3 = new Array() document.write(ar3.getType()) // object var ar4 = new Array(0) document.write(ar4.getType()) // object var ar5 = new Array(1) ar5[9999] = 5 document.write(ar5.getType()) // mixed
If you tried out the fifth array, you probably had to wait for a while, because the loop executed 10,000 times! You can use a more efficient function for the same prototype:
function getType() { var arrayType = typeof this[0] for (var i in this) { if (typeof this[i] != arrayType) { arrayType = "mixed" break } } return arrayType } Array.prototype.getType = getType
The improvement in this function is the type of loop used. The for...in construct loops only through the existing properties, or elements, not including the null ones. The function works just like the previous one. At first, the data type of the first element of the array is assigned to arrayType. In every iteration of the loop, if a different type element is found, the value of arrayType is changed to “mixed” because at least two different data types have been found in the array. Once a “mixed” array is detected, the loop is immediately terminated using a break statement. The function returns the value held by arrayType.
|
|||||||||||||||||||||||
With any suggestions or questions please feel free to contact us |