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.

In addition to the direct way elements may be accessed, <strong>JavaScript</strong>‘s for/in construct is perfect<br />

for iterating over the elements of associative arrays. <strong>The</strong> following example loops through all<br />

elements of an array and prints them out:<br />

var customers = new Object();<br />

customers["Tom Doe"] = "123 Main St., Metropolis, USA";<br />

customers["Sylvia Cheung"] = "123 Fake St., Vancouver B.C., Canada";<br />

customers["George Speight"] = "145 Baldwin St., Dunedin, NZ";<br />

for (var client in customers)<br />

{<br />

}<br />

document.writeln("<strong>The</strong> address of client " + client + " is:");<br />

document.writeln(customers[client]);<br />

document.writeln("");<br />

Each name that has data associated with it is assigned to client, one at a time. This variable is<br />

used to access the data in the array. <strong>The</strong> output of the previous example is shown in Figure 6-<br />

4.<br />

Figure 6-4: Associative arrays provide key/value data lookup capabilities in <strong>JavaScript</strong>.<br />

Now that we‘ve covered the fundamentals of how objects behave and how you can create and<br />

manipulate generic objects, it‘s time to explore <strong>JavaScript</strong>‘s object-oriented features. <strong>The</strong>se<br />

features enable you to structure your scripts in a mature fashion similar to more mainstream<br />

application development languages such Java and C++. <strong>JavaScript</strong>‘s object-oriented features<br />

aren‘t as flexible as these languages, but you‘ll probably find that what <strong>JavaScript</strong> has to offer is<br />

well suited to the kinds of tasks required when writing large scripts or building Web-based<br />

applications.

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

Saved successfully!

Ooh no, something went wrong!