Main Page

Native objects

Types of Objects
In ECMAScript, all objects are not created equal. Generally speaking, three specific types of objects can
be used and/or created.
Native objects
ECMA-262 defines
native objects
as “any object supplied by an ECMAScript implementation indepen-
dent of the host environment.” Simply put, native objects are the classes (reference types) defined by
ECMA-262. They include all the following:
Object Function Array String
Boolean Number Date RegExp
Error EvalError RangeError ReferenceError
SyntaxError TypeError URIError
Some of these native objects you are already familiar with from the previous chapter (
Object
,
Function
,
String
,
Boolean
, and
Number
), and some will be discussed later in the book. For now, the two native
objects of importance are
Array
and
Date
.
The Array class
In ECMAScript, unlike in Java, there is an actual
Array
class. You create an
Array
object like this:
var aValues = new Array();
If you know ahead of time how many items you need in the array, you can pass in the array size as a
parameter:
var aValues = new Array(20);
Using either of these two methods, you must populate the array by using bracket notation, similar to
how it is done in Java:
var aColors = new Array();
aColors[0] = “red”;
aColors[1] = “green”;
aColors[2] = “blue”;
Here, an array is created and given three items,
“red”
,
“green”
, and
“blue”
. The array dynamically
grows in size with each additional item
Also, if you know the values that the array should contain, you can specify those as arguments, creating
an
Array
object with a length equal to the number of arguments. For example, the following line of code
creates an array of three strings:
var aColors = new Array(“red”, “green”, “blue”);
70
Chapter 3
06_579088 ch03.qxd 3/28/05 11:36 AM Page 70


JavaScript EditorFree JavaScript Editor     Ajax Editor


©

R7