11.12.2012 Views

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>The</strong> objects in <strong>JavaScript</strong> are summarized in Table 6-1.<br />

Table 6-1: <strong>The</strong> Four Types of Objects Available to <strong>JavaScript</strong><br />

Type Example Implementation<br />

Provided By<br />

User-defined Programmerdefined<br />

Customer<br />

or Circle<br />

Programmer None<br />

Built-in Array, Math <strong>The</strong> browser via<br />

its <strong>JavaScript</strong><br />

engine<br />

Governing<br />

Standard<br />

ECMA-262<br />

Browser Window, Navigator <strong>The</strong> browser None (though<br />

some portions<br />

adhere to an ad<br />

hoc standard)<br />

Document Image,<br />

HTMLInputElement<br />

<strong>The</strong> browser via<br />

its DOM engine<br />

W3C DOM<br />

<strong>The</strong>re is some overlap in these four categories. For example, before the advent of the official<br />

DOM standard, objects such as Image were browser objects because each vendor<br />

implemented their own feature set. <strong>The</strong> major reason there is such overlap is that there is no<br />

one standard governing how all aspects of <strong>JavaScript</strong> are supposed to behave. <strong>The</strong> ECMA-262<br />

standard governs the nuts and bolts of the language itself. <strong>The</strong> W3C‘s DOM specification<br />

dictates how structured documents like Web pages should be presented to a scripting<br />

environment. Browser vendors define access to the user interface as they see fit and even<br />

create their own proprietary extensions to the DOM. <strong>The</strong> result is a chaotic and somewhat<br />

confusing set of technologies that come together under the umbrella of ―<strong>JavaScript</strong>.‖<br />

<strong>The</strong> good news is that browser vendors have finally settled on a de facto standard for browser<br />

objects. This ―standard‖ is more an artifact of historical circumstances and browser wars than<br />

the product of a rational design process. This is evidenced by the fact that the Navigator object<br />

is supported by Opera, Netscape, and Internet Explorer despite obviously deriving its name<br />

from Netscape‘s original Navigator browser. While the features implemented by Navigator<br />

objects are somewhat consistent across browsers, a close examination reveals some variation<br />

in the support of its properties by the different browser types and versions.<br />

This chapter covers the fundamental ways that objects behave and can be manipulated in<br />

<strong>JavaScript</strong>. <strong>The</strong> specific capabilities of built-in, browser, and document objects are discussed in<br />

detail in chapters that follow.<br />

Object Fundamentals<br />

An object is an unordered collection of data, including primitive types, functions, and even other<br />

objects. <strong>The</strong> utility of objects is that they gather all the data and logic necessary for a particular<br />

task in one place. A String object stores textual data and provides many of the functions you<br />

need to operate upon it. While objects aren‘t strictly necessary in a programming language (for<br />

example, C has no objects), they definitely make a language that contains them easier to use.<br />

Object Creation<br />

An object is created with a constructor, a special type of function that prepares a new object for<br />

use by initializing the memory it takes up. In Chapter 4, we saw how objects are created by<br />

applying the new operator to their constructors. This operator causes the constructor to which it<br />

is applied to create a brand-new object, and the nature of the object that is created is<br />

determined by the particular constructor that is invoked. For example, the String() constructor

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!