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.

queue.push(20); // [10, 20]<br />

queue.shift(); // [20] Returns 10<br />

queue.shift(); // [] Returns 20<br />

Even if you never use arrays as stacks or queues, the methods discussed in this section can<br />

come in handy to manipulate the contents of arrays. Now let‘s look at a few more useful array<br />

manipulations.<br />

Note As mentioned at the start of the chapter, these methods require <strong>JavaScript</strong> 1.2 or JScript<br />

5.5 or better. Internet Explorer 5 and earlier will not be able to natively use these features.<br />

However, using an Array prototype to add our own pop() and push() methods can fix this<br />

problem. See the section entitled ―Extending Arrays with Prototypes,‖ later in this chapter.<br />

Manipulating Arrays<br />

<strong>JavaScript</strong> provides a wealth of methods for carrying out common operations on arrays. This<br />

section provides an overview of these Array methods with a brief discussion of some of their<br />

quirks.<br />

concat() Method<br />

<strong>The</strong> concat() method returns the array resulting from appending its arguments to the array on<br />

which it was invoked. Given the script:<br />

var myArray = ["red", "green", "blue"];<br />

alert(myArray.concat("cyan", "yellow"));<br />

the expected larger array is shown here:<br />

Be careful, though; concat() does not modify the array in place. Notice the output of this script,<br />

var myArray = ["red", "green", "blue"];<br />

myArray.concat("cyan", "yellow");<br />

alert(myArray);<br />

which is shown here:

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

Saved successfully!

Ooh no, something went wrong!