JavaScript functions are the developer s most basic tool for creating structured reusable code. A function should be designed to be self-contained and pass data in through parameters and back via the
return
statement. In general, most parameters are passed to a function by value, but composite types such as arrays and objects are passed by reference. JavaScript functions are very flexible and a variable number of parameters can be passed to a function. However, some programming caution should be employed, given JavaScript s lax type and parameter checking. Further, to ensure reusable functions, local
JavaScript is an object-based language. With the exception of language constructs like
Objects in JavaScript fall into four groups:
Built-in
objects are provided by the JavaScript language itself. These include those objects associated with data types (
String
,
Number
, and
Boolean
), objects that allow creation of user-defined objects and composite types (
Object
and
Array
), and objects that simplify common tasks, such as
Date
,
Math
, and
RegExp
. The capabilities of built-in objects are governed by the ECMA-262 language standard and, to a lesser extent, by the specifications of particular browser
Browser
objects are those objects not specified as part of the JavaScript language but that most browsers commonly support. Examples of browser objects include
Window
, the object that enables the manipulation of browser
Document
objects are part of the Document Object Model (DOM), as defined by the W3C. These objects present the programmer with a structured interface to (X)HTML and XML documents. It is these objects that enable JavaScript to manipulate Cascading Style Sheets (CSS) and that facilitate the realization of Dynamic HTML (DHTML). Access to the document objects is provided by the browser via the
document
property of the
Window
object (
window.document
). An
The objects in JavaScript are summarized in Table 6-1.
|
Type |
Example |
Implementation Provided By |
|
|---|---|---|---|
|
User-defined |
Programmer-defined Customer or Circle |
Programmer |
None |
|
Built-in |
Array, Math |
The browser via its JavaScript engine |
ECMA-262 |
|
Browser |
Window, Navigator |
The browser |
None (though some portions
|
|
Document |
Image, HTMLInputElement |
The browser via its DOM engine |
W3C DOM |
There is some overlap in these four categories. For example, before the
The good news is that browser vendors have finally settled on a de facto standard for browser objects. This standard is more an artifact of historical circumstances and browser wars than the product of a rational design process. This is evidenced by the fact that the
Navigator
object is supported by Opera, Netscape, and Internet Explorer despite obviously deriving its
This chapter covers the fundamental ways that objects behave and can be manipulated in JavaScript. The specific capabilities of built-in, browser, and document objects are discussed in detail in chapters that follow.