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.

alert(myArray[2]);<br />

alert(myArray[3]);<br />

In the previous example, we created an Array object using an array literal. We could have also<br />

used the new operator to do so. For example:<br />

var myArray = new Array(2, 4, 8, 10);<br />

<strong>The</strong> new operator is used to create objects. It can be used both to create user-defined objects<br />

and to create instances of built-in objects. <strong>The</strong> following script creates a new instance of the<br />

Date object and places it in the variable today.<br />

var today = new Date();<br />

alert(today);<br />

<strong>The</strong> result is shown here:<br />

Most languages that allow you to create an object with new allow you to destroy one with<br />

delete. This isn‘t quite true of <strong>JavaScript</strong>. To destroy an object, you set it to null. For example,<br />

to destroy the object in the previous example, you would write<br />

today = null;<br />

In <strong>JavaScript</strong>, the delete operator is used to remove a property from an object and to remove<br />

an element from an array. <strong>The</strong> following script illustrates its use for the latter purpose:<br />

var myArray = ['1', '3', '78', '1767'];<br />

document.write("myArray before delete = " + myArray);<br />

document.write("");<br />

delete myArray[2];<br />

// deletes third item since index starts at 0<br />

document.write("myArray after delete = " + myArray);<br />

Notice that the third item, 78, has been removed from the array:

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

Saved successfully!

Ooh no, something went wrong!