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.

within Web sites. Most <strong>JavaScript</strong> programmers use those features they find convenient and<br />

leave the major OOP features to those writing full-fledged Web applications.<br />

Summary<br />

<strong>JavaScript</strong> provides four types of objects: user-defined, built-in, browser, and document. This<br />

chapter focused on the fundamental aspects of all objects, as well as the creation and use of<br />

user-defined objects. <strong>JavaScript</strong> is a prototype-based, object-oriented language. New object<br />

instances are created with constructors, objects that initialize the properties of new instances.<br />

Every object has a prototype property that reflects the prototype of the constructor used to<br />

create it. When an object property is accessed, the interpreter first checks the object‘s instance<br />

properties for the desired name. If it is not found, the properties of the object‘s prototype are<br />

checked. This process repeats recursively until it has worked up the chain of inheritance to the<br />

top-level object. Most of the time in <strong>JavaScript</strong>, the creation and management of the objects is<br />

straightforward, and programmers are freed from such headaches as memory management.<br />

While user-defined objects can be used to create much more modular and maintainable scripts,<br />

many <strong>JavaScript</strong> programmers do not really use them, given the simplicity of their scripts.<br />

Instead, the various built-in, browser, and document objects are utilized. <strong>The</strong> next chapter<br />

begins the examination of such objects, starting with built-in objects, particularly Array, Math,<br />

Date, and String.<br />

Chapter 7: Array, Date, Math, and Type-Related<br />

Objects<br />

This chapter discusses in detail the capabilities of <strong>JavaScript</strong>‘s built-in objects, particularly<br />

Array, Date, and Math. We will also look into the built-in objects related to the primitive types,<br />

such as Boolean, Number, and String, as well as the mysterious Global object. Notably<br />

missing from this chapter is the RegExp object, which requires a significant amount of<br />

explanation and is the subject of the next chapter. For each object covered in this chapter, the<br />

focus will be primarily on those properties most commonly used and supported by the major<br />

browsers. <strong>The</strong> complete list of properties of the built-in objects, including version information,<br />

can be found in Appendix B. So let‘s start our overview of these built-in objects, proceeding in<br />

alphabetical order, starting from Array and ending in String.<br />

Array<br />

Arrays were introduced in Chapter 3 as composite types that store ordered lists of data. Arrays<br />

may be declared using the Array() constructor. If arguments are passed to the constructor, they<br />

are usually interpreted as specifying the elements of the array. <strong>The</strong> exception is when the<br />

constructor is passed a single numeric value that creates an empty array, but sets the array‘s<br />

length property to the given value. Three examples of array declaration are<br />

var firstArray = new Array();<br />

var secondArray = new Array("red", "green", "blue");<br />

var thirdArray = new Array(5);<br />

<strong>The</strong> first declaration creates an empty array called firstArray. <strong>The</strong> second declaration creates a<br />

new array secondArray with the first value equal to ―red,‖ the second value equal to ―green,‖<br />

and the last value equal to ―blue.‖ <strong>The</strong> third declaration creates a new empty array thirdArray<br />

whose length property has value 5. <strong>The</strong>re is no particular advantage to using this last syntax,<br />

and it is rarely used in practice.<br />

<strong>JavaScript</strong> 1.2+ allows you to create arrays using array literals. <strong>The</strong> following declarations are<br />

functionally equivalent to those of the previous example:

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

Saved successfully!

Ooh no, something went wrong!