spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / java_core / 1 current pageTo page 2To page 3To page 4To page 5To page 6To page 7
[next]

Web Project Manager
Aquent
US-PA-Collegeville

Justtechjobs.com Post A Job | Post A Resume
Developer News
Mandrake Linux Founder Back, Virtually
Amazon: We're a Technology Company
Sun Expands MySQL With Closed Source

JavaScript by Example: JavaScript Core Objects. Pt. 1

This book excerpt is from Ellie Quigley's "JavaScript by Example" ISBN 0131401629.
All rights reserved. JavaScript Core Objects is posted with permission Prentice Hall.

What Are Core Objects?

Like an apple, JavaScript has a core, and at its core are objects. Everything you do in JavaScript will be based on objects; you may create your own or use JavaScript's core objects, those objects built right into the language. JavaScript provides built-in objects that deal with date and time, math, strings, regular expressions, numbers, and other useful entities. The good news is that the core objects are consistent across different implementations and platforms and have been standardized by the ECMAScript 1.0 specification, allowing programs to be portable. Although each object has a set of properties and methods that further define it, this book does not detail every one, but highlights those that will be used most often. For a complete list of properties and objects, see the CD-ROM in the back of this book or go to http://developer.netscape.com.

Array Objects

An array is a collection of like values--called elements--such as an array of colors, an array of strings, or an array of images. Each element of the array is accessed with an index value enclosed in square brackets (see Figure 9.1). An index is also called a subscript. There are two types of index values: a non-negative integer and a string. Arrays indexed by strings are called associative arrays.1 In JavaScript, arrays are built-in objects with some added functionality.2

An Array object called color. Index values are in square brackets.

Declaring an Array

Like variables, arrays must be declared before they can be used. The new keyword is used to dynamically create the Array object. It calls the Array object's constructor, Array(), to create a new Array object. The size of the array can be passed as an argument to the constructor, but it is not necessary. Values can also be assigned to the array when it is constructed, but this is not required either. Let's examine some ways to create an array.

The following array is called array_name and its size is not specified.

 

var array_name = new Array();

 

In the next example, the size or length of the array is passed as an argument to the Array() constructor. The new array has 100 undefined elements.

 

var array_name = new Array(100);

 

And in the next example, the array is given a list of initial values of any data type:

 

var array_name = new Array("red", "green", "yellow", 1 ,2, 3);

 

Although you can specify the size of the array when declaring it, it is not required. JavaScript allocates memory as needed to allow the array to shrink and grow on demand. To populate the array, each element is assigned a value. Each element is indexed by either a number or string. If the array index is a number, it starts with 0. JavaScript doesn't care what you store in the array. Any combination of types, such as numbers, strings, Booleans, and so forth, are acceptable. quigley09.htm#28991 creates a new Array object called book and assigns strings to each of its elements.

Using the new Constructor

To create an Array object, call the Array() constructor with the new keyword and pass information to the constructor if you know the size and/or what elements you want to assign to the array. Values can be added or deleted throughout the program; JavaScript provides a number of methods to manipulate the array (these are listed in See Array Methods).

 
 

 

<html>

<head><title>The Array Object</title>

<h2>An Array of Books</h2>

<script language="JavaScript">

1 var book = new Array(6); // Create an Array object

2 book[0] = "War and Peace"; // Assign values to its elements

book[1] = "Huckleberry Finn";

book[2] = "The Return of the Native";

book[3] = "A Christmas Carol";

book[4] = "The Yearling";

book[5] = "Exodus";

</script>

</head>

<body bgcolor="lightblue">

<script language="JavaScript">

document.write("<h3>");

3 for(var i in book){

4 document.write("book[" + i + "] "+ book[i] + "<br>");

}

</script>

</body>

</html>

 

 
The variable book is assigned a new Array object containing six elements.
  1. The first element of the book array is assigned the string "War and Peace". Array indices start at zero.

  2. The special for loop is used to access each of the elements in the book array.

  3. Each of the elements of the book array are displayed in the browser. (See Figure 9.2.)

 

home / programming / java_core / 1 current pageTo page 2To page 3To page 4To page 5To page 6To page 7
[next]

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Whitepapers and eBooks

Symantec Whitepaper: Converging System and Data Protection for Complete Disaster Recovery
Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
  Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Symantec Whitepaper: Comprehensive Backup and Recovery of VMware Virtual Infrastructure
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP · Working with the DOM Stylesheets Collection
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Fixing MySQL Replication · Firewall Guide: First Steps to Securing the Enterprise · VoxOx Tames the Tumultuous Communications Tangle

Created: March 27, 2003
Revised: November 19, 2003

URL: http://webreference.com/programming/java_core/1