04.11.2015 Views

javascript

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 5: Reference Types<br />

The Array Type<br />

After the Object type, the Array type is probably the most used in ECMAScript. An ECMAScript array<br />

is very different from arrays in most other programming languages. As in other languages, arrays are<br />

ordered lists of data, but unlike in other languages, they can hold any type of data in each slot. This<br />

means that it ’ s possible to create an array that has a string in the first position, a number in the second,<br />

an object in the third, and so on. ECMAScript arrays are also dynamically sized, automatically growing<br />

to accommodate any data that is added to them.<br />

Arrays can be created in two basic ways. The first is to use the Array constructor, as in this line:<br />

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

If you know the number of items that will be in the array, you can pass the count into the constructor,<br />

and the array will automatically be created with that number of slots (each of the items is initialized with<br />

the value undefined ). For example, the following creates an array with 20 items:<br />

var colors = new Array(20);<br />

The Array constructor can also be passed items that should be included in the array. The following<br />

creates an array with three string values:<br />

var colors = new Array(“red”, “blue”, “green”);<br />

An array can be created with a single value by passing it into the constructor. This gets a little bit tricky<br />

because providing a single argument that is a number always creates an array with the given number of<br />

items, whereas an argument of any other type creates a one - item array that contains the specified value.<br />

Here ’ s an example:<br />

var colors = new Array(3); //create an array with three items<br />

var names = new Array(“Greg”); //create an array with one item, the string “Greg”<br />

It ’ s possible to omit the new operator when using the Array constructor. It has the same result, as you<br />

can see here:<br />

var colors = Array(3); //create an array with three items<br />

var names = Array(“Greg”); //create an array with one item, the string “Greg”<br />

The second way to create an array is by using array literal notation. An array literal is specified by using<br />

square brackets and placing a comma - separated list of items between them, as in this example:<br />

var colors = [“red”, “blue”, “green”]; //creates an array with three strings<br />

var names = [];<br />

//creates an empty array<br />

var values = [1,2,];<br />

//AVOID! Creates an array with 2 or 3 items<br />

var options = [,,,,,];<br />

//AVOID! creates an array with 5 or 6 items<br />

In this code, the first line creates an array with three string values. The second line creates an empty<br />

array by using empty square brackets. The third line shows the effects of leaving a comma after the last<br />

value in an array literal: In IE, values becomes a three - item array containing the values 1, 2, and<br />

undefined ; in all other browsers, values is a two - item array containing the values 1 and 2.<br />

This is due to a bug regarding array literals in the IE implementation of ECMAScript. Another instance<br />

100

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

Saved successfully!

Ooh no, something went wrong!