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 />

Perhaps the most powerful array method is splice() , which can be used in a variety of ways. The main<br />

purpose of splice() is to insert items into the middle of an array, but there are three distinct ways of<br />

using this method. They are as follows:<br />

Deletion — Any number of items can be deleted from the array by specifying just two<br />

arguments: the position of the first item to delete and the number of items to delete. For<br />

example, splice(0, 2) deletes the first two items.<br />

Insertion — Items can be inserted into a specific position by providing three arguments: the<br />

starting position, 0 (the number of items to delete), and the item to insert. Optionally, you can<br />

specify a fourth, fifth, or any number of other parameters to insert. For example, splice(2, 0,<br />

“ red ” , “ green “ ) inserts the strings “ red ” and “ green ” into the array at position 2.<br />

Replacement — Items can be inserted into a specific position while simultaneously deleting<br />

items if you specify three arguments: the starting position, the number of items to delete, and<br />

any number of items to insert. The number of items to insert doesn ’ t have to match the number<br />

of items to delete. For example, splice(2, 1, “ red ” , “ green “ ) deletes one item at position<br />

2 and then inserts the strings “ red ” and “ green ” into the array at position 2.<br />

The splice() method always returns an array that contains any items that were removed from the array<br />

(or an empty array if no items were removed). These three uses are illustrated in the following code:<br />

var colors = [“red”, “green”, “blue”];<br />

var removed = colors.splice(0,1);<br />

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

alert(removed); //red - one item array<br />

//remove the first item<br />

removed = colors.splice(1, 0, “yellow”, “orange”); //insert two items at position 1<br />

alert(colors); //green,yellow,orange,blue<br />

alert(removed); //empty array<br />

removed = colors.splice(1, 1, “red”, “purple”);<br />

alert(colors); //green,red,purple,orange,blue<br />

alert(removed); //yellow - one item array<br />

//insert two values, remove one<br />

This example begins with the colors array containing three items. When splice is called the first time, it<br />

simply removes the first item, leaving colors with the items “ green ” and “ blue ” . The second time<br />

splice() is called, it inserts two items at position 1, resulting in colors containing “ green ” ,<br />

“ yellow ” , “ orange ” , and “ blue ” . No items are removed at this point, so an empty array is returned.<br />

The last time splice() is called, it removes one item, beginning in position 1, and inserts “ red ” and<br />

“ purple ” . After all of this code has been executed, the colors array contains “ green ” ,<br />

“ red ” , “ purple ” , “ orange ” , and “ blue ” .<br />

The Date Type<br />

The ECMAScript Date type is based on an early version of java.util.Date from Java. As such, the<br />

Date type stores dates as the number of milliseconds that have passed since midnight on January 1, 1970<br />

UTC (Universal Time Code). Using this data storage format, the Date type can accurately represent<br />

dates 285,616 years before or after January 1, 1970.<br />

109

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

Saved successfully!

Ooh no, something went wrong!