Chapter 6: Objects


JavaScript is an object-based language. With the exception of language constructs like loops and relational operators, almost all of JavaScript s features are implemented using objects in one way or another. Sometimes objects are used explicitly to carry out certain tasks , such as the manipulation of (X)HTML and XML documents using the Document Object Model. Other times, the role of objects in the language is less obvious, like the role played by the String object during the manipulation of primitive string data. While previous chapters presented examples that implicitly demonstrated the use of built-in objects, this chapter will explore JavaScript objects directly.

Objects in JavaScript

Objects in JavaScript fall into four groups:

  • User -defined objects are custom objects created by the programmer to bring structure and consistency to a particular programming task. This chapter covers the creation and use of such objects.

  • 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 vendors . The following two chapters discuss the features of built-in objects.

  • 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 windows and interaction with the user, and Navigator , the object that provides information about client configuration. Because most aspects of browser objects are not governed by any standard, their properties and behavior can vary significantly from browser to browser and from version to version. These types of objects will be discussed throughout the rest of the book and in Chapter 9 particularly.

  • 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 in-depth discussion of the DOM can be found in Chapter 10.

The objects in JavaScript are summarized in Table 6-1.

Table 6-1: The Four Types of Objects Available to JavaScript

Type

Example

Implementation Provided By

Governing Standard

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 adhere to an ad hoc standard)

Document

Image, HTMLInputElement

The browser via its DOM engine

W3C DOM

There is some overlap in these four categories. For example, before the advent of the official DOM standard, objects such as Image were browser objects because each vendor implemented their own feature set. The major reason there is such overlap is that there is no one standard governing how all aspects of JavaScript are supposed to behave. The ECMA-262 standard governs the nuts and bolts of the language itself. The W3C s DOM specification dictates how structured documents like Web pages should be presented to a scripting environment. Browser vendors define access to the user interface as they see fit and even create their own proprietary extensions to the DOM. The result is a chaotic and somewhat confusing set of technologies that come together under the umbrella of JavaScript.

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 name from Netscape s original Navigator browser. While the features implemented by Navigator objects are somewhat consistent across browsers, a close examination reveals some variation in the support of its properties by the different browser types and versions.

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.




JavaScript 2.0
JavaScript: The Complete Reference, Second Edition
ISBN: 0072253576
EAN: 2147483647
Year: 2004
Pages: 209

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net