18.04.2016 Views

Professional JavaScript For Web Developers

javascript for learners.

javascript for learners.

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.

Object Basics<br />

As in strings, the first item in an array is in position 0, the second is in position 1, and so on. To access a<br />

particular item, use square brackets enclosing the position of the item to retrieve. <strong>For</strong> instance, to output<br />

the string “green” from the array defined previously, you do this:<br />

alert(aColors[1]);<br />

//outputs “green”<br />

The full size of the array can be determined by using the length property. Like the same property in<br />

strings, the length property is always one more than the position of the last item, meaning that an array<br />

with three items has items in positions 0 through 2.<br />

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

alert(aColors.length); //outputs “3”<br />

As mentioned previously, the size of an array can grow and shrink as necessary. So, if you wanted to add<br />

another item to the array defined previously, you can just place the value in the next open position:<br />

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

alert(aColors.length); //outputs “3”<br />

aColors[3] = “purple”;<br />

alert(aColors.length); //outputs “4”<br />

In this code, the next open position is 3, so the value “purple” is assigned to it. This addition changes<br />

the length of the array from 3 to 4. But what would happen if you placed a value in position 25 of this<br />

array? ECMAScript fills in all positions from 3 to 24 with the value null; then it places the appropriate<br />

value in position 25, increasing the size of the array to 26:<br />

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

alert(aColors.length); //outputs “3”<br />

aColors[25] = “purple”;<br />

aColors(arr.length); //outputs “26”<br />

Arrays can contain a maximum of 4294967295 items, which should be plenty for<br />

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

occurs.<br />

You can also define an Array object by using the literal representation, which is indicated by using<br />

square brackets ([ and ]) and separating the values with commas. <strong>For</strong> instance, the previous example can<br />

be rewritten in the following form:<br />

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

alert(aColors.length); //outputs “3”<br />

aColors[25] = “purple”;<br />

alert(aColors.length); //outputs “26”<br />

Note that, in this case, the Array class is never mentioned explicitly. The square brackets imply that the<br />

enclosed values are to be made into an Array object. Arrays declared in this way are exactly equal to<br />

arrays declared in the more traditional manner.<br />

71

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

Saved successfully!

Ooh no, something went wrong!