↑
Main Page
Date class
This code sorts the strings
“red”
,
“green”
,
“blue”
, and
“yellow”
into alphabetical order by using
their character codes. Because all values are strings, this sort order is logical. If, however, the values are
numbers, the result becomes bizarre:
var aColors = [3, 32, 2, 5]
aColors.sort();
alert(aColors.toString()); //outputs “2,3,32,5”
When trying to sort the numbers 3, 32, 2, and 5, the
sort()
method reorders the items into 2, 3, 32, and
5. As mentioned before, this occurs because the numbers are converted to strings and then compared by
character code. This problem can be overcome. I discussed this further in Chapter 12, “Sorting Tables.”
The most complicated method by far is
splice()
. The purpose of this method is quite simple really: to
insert items into the middle of an array. The variety of ways that
splice()
uses to insert these items,
however, takes some getting used to:
?
Deletion
— You can delete any number of items from the array by specifying just two parame-
ters, the starting position of the first item to delete and the number of items to delete. For exam-
ple:
arr.splice(0, 2)
deletes the first two items in the array
arr
.
?
Replacement without delete
— You can insert items into a specific position by specifying three
parameters: the starting position, 0 (the number of items to delete), and the item to insert. You
can optionally specify fourth, fifth, or any number of other parameters to insert. For example,
arr.splice(2, 0, “red”, “green”)
inserts the strings
“red”
and
“green”
into the array
arr
at position 2.
?
Replacement with delete
— You can insert items into a specific position while simultaneously
deleting items by specifying three parameters: the starting position, the number of items to
delete, and the item to insert. Here, you can also specify extra parameters to insert. The
number of items to insert doesn’t have to match the number of items to delete. For example,
arr.splice(2, 1, “red”, “green”)
deletes one item at position
2
and then inserts the
strings
“red”
and
“green”
into the array
arr
at position
2
.
As you can tell, the
Array
class is an extremely versatile and helpful object. Chapter 12 explores using
arrays in a more practical manner, but for now, this information is all you need to know.
The Date class
The
Date
class in ECMAScript is based on earlier versions of
java.util.Date
from Java. ECMAScript,
as well as Java, stores the date as the number of milliseconds since 12 AM on January 1, 1970 UTC. UTC
stands for Universal Time Code (also known as Greenwich Mean Time), which is the standard time
upon which all time zones are based. Storing the number of milliseconds ensures that both Java and
ECMAScript were immune from the dreaded “Y2K” problems that plagued older mainframe computers
in the late 1990s. Dates can accurately be represented 285,616 years before or after January 1, 1970, mean-
ing that you won’t have any problems with date storage unless you live to be over 200,000 years old.
To create a new
Date
object, you simply do the following:
var d = new Date();
77
Object Basics
06_579088 ch03.qxd 3/28/05 11:36 AM Page 77
Free JavaScript Editor
Ajax Editor
©
→ R7