Please send questions to
st10@humboldt.edu .
Session 6, 9-7-06 - Very random projected notes
Arrays...
* more like a Perl array than a C/C++/Java array --- more flexible
* to declare one --- just create a new Array object...
ex: var stuff = new Array()
* you can give it a size, if you want:
var tenThings = new Array(10)
* note that those 10 "holes" are currently holding the special
value undefined
* arrays are indexed like C/C++/Java ---
tenThings[0] is the FIRST element in array tenThings,
tenThings[1] is the SECOND element in array tenThings
...
tenThings[9] is the TENTH element in array tenThings
...and you can grab, set elements in the array using these
indices:
tenThings[5] = "Hello", then the element at index 5 (the 6th
element) now is the string "Hello"
document.write( tenThings[5] ) --> print out Hello
* are numerous array properties and methods..
tenThings.length --> how many elements are in the array
* for loops are lovely with arrays!
* traditional: just like C/C++/Java:
for (var i = 0; i < array.length; i++)
{
... array[i] ...
}
* for .. in - to iterate through the elements of an array
or the properties of an object
for (i in array)
{
... array[i] ...
}
* BUT... this seems to SKIP the undefined elements in array,
in my playing around
* see js_arr1.html
* you can fill the array when you declare it, if you want to...
var days = new Array("mon", "tue", "wed", "thu", "fri", "sat", "sun")
* Array literal: [ elem, elem, ... elem]
[1, 2, "george", 5] <-- 4 element Array object
var days = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"]
* ASSOCIATIVE arrays --- can reference by NAME as well as by INDEX...
(example from
"JavaScript Concepts and Techniques - Programming Interactive
Web Sites", by Tina Spain McDuffie, Franklin, Beedle and
Associates, 2003, pp. 196-197)
<body>
<img src="products.gif" name="products">
<img src="services.gif" name="services">
<img src="contact.gif" name="contact">
...
<script type="text/javascript">
...
document.images[0]
document.images[1]
document.images[2]
document.images["products"]
document.images["services"]
document.images["contact"]
* String object - many useful methods available!!
* Date object - ditto;
* Math object - useful methods, too;
...and links to the W3Schools JavaScript Reference pages for
Array, String, Date, and Math will be added to the JavaScript
references page on the public course web site...