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 (
, and
), and some will be discussed later in the book. For now, the two native
objects of importance are
The Array class
In ECMAScript, unlike in Java, there is an actual
class. You create an
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
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,
, and
. 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
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”);
Chapter 3
06_579088 ch03.qxd 3/28/05 11:36 AM Page 70

JavaScript EditorFree JavaScript Editor     Ajax Editor