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 length property can also be helpful in adding items to the end of an array, as in this example:<br />

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

colors[colors.length] = “black”; //add a color (position 3)<br />

colors[colors.length] = “brown”; //add another color (position 4)<br />

T he last item in an array is always at position length – 1 , so the next available open slot is at position<br />

length . Each time an item is added after the last one in the array, the length property is automatically<br />

updated to reflect the change. That means colors[colors.length] assigns a value to position 3 in the<br />

second line of this example and to position 4 in the last line. The new length is automatically calculated<br />

when an item is placed into a position that ’ s outside of the current array size, which is done by adding 1<br />

to the position, as in this example:<br />

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

colors[99] = “black”; //add a color (position 99)<br />

alert(colors.length); //100<br />

In this code, the colors array has a value inserted into position 99, resulting in a new length of 100<br />

(99 + 1). Each of the other items, positions 3 through 98, is filled with undefined .<br />

Arrays can contain a maximum of 4,294,967,295 items, which should be plenty<br />

for almost all programming needs. If you try to add more than that number, an<br />

exception occurs. Trying to create an array with an initial size approaching this<br />

maximum may cause a long - running script error.<br />

Conversion Methods<br />

As mentioned previously, all objects have toLocaleString() , toString() , and valueOf() methods.<br />

The toString() and valueOf() methods return the same value when called on an array. The result is a<br />

comma - separated string that contains the string equivalents of each value in the array, which is to say<br />

that each item has its toString() method called to create the final string. Take a look at this example:<br />

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

alert(colors.toString()); //red,blue,green<br />

alert(colors.valueOf()); //red,blue,green<br />

alert(colors);<br />

//red,blue,green<br />

In this code, the toString() and valueOf() methods are first called explicitly to return the string<br />

representation of the array, which combines the strings, separating them by commas. The last line passes<br />

the array directly into alert() . Because alert() expects a string, it calls toString() behind the<br />

scenes to get the same result as when toString() is called directly.<br />

The toLocaleString() method may end up returning the same value as toString() and valueOf() ,<br />

but not always. When toLocaleString() is called on an array, it creates a comma - delimited string of<br />

the array values. The only difference between this and the two other methods is that toLocaleString()<br />

102

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

Saved successfully!

Ooh no, something went wrong!