The Array Object


The Array Object

We've already seen the Array object in Chapter 2 (see the section "Arrays" in Chapter 2), but we haven't taken an in-depth look at the Array object itself. We'll do that here. You can see an overview of the properties and methods of the Array object in Table 19.9, its properties in depth in Table 19.10, and its methods in depth in Table 19.11.

Table 19.9. Overview of the Properties and Methods of the Array Object

Properties

Methods

constructor

concat

length

join

prototype

pop

 

push

 

reverse

 

shift

 

slice

 

sort

 

splice

 

toLocaleString

 

toString

 

unshift

Table 19.10. The Properties of the Array Object

Property

NS2

NS3

NS4

NS6

IE3a

IE3b

IE4

IE5

IE5.5

IE6

constructor

 

x

x

x

 

x

x

x

x

x

 

Read/write

 

This property specifies the function that creates an object.

length

 

x

x

x

 

x

x

x

x

x

 

Read/write

 

This property holds an integer value one higher than the index of the highest element in an array. Note that because elements don't have to be contiguous, this property does not technically have to reflect the total number of elements in an array!

prototype

 

x

x

x

 

x

x

x

x

x

 

Read/write

 

This property returns a reference to the object's prototype. You use the prototype property to provide base functionality to a class of objects. See "Using the prototype Property" in this chapter.

Table 19.11. The Methods of the Array Object

Method

NS2

NS3

NS4

NS6

IE3a

IE3b

IE4

IE5

IE5.5

IE6

concat

   

x

x

   

x

x

x

x

 

Returns: Array object

 

This method concatenates (joins) two arrays together and returns the result.

 

Syntax: array1 .concat( array2 ) .

join

 

x

x

x

 

x

x

x

x

x

 

Returns: String

 

This method returns a string holding all the elements of an array joined together, separated by a separator string.

 

Syntax: array .join( separator ) , where separator is the separator string you want to appear between elements in the returned string.

pop

   

x

x

       

x

x

 

Returns: Array entry

 

This method "pops" the array by removing the last element from an array and returning it. See "Pushing and Popping" in this chapter.

 

Syntax: array .pop() .

push

   

x

x

       

x

x

 

Returns: Array entry

 

This method "pushes" a new element or elements onto an array by appending them to the end of the array and returning the new length of the array.

 

Syntax: array .push( item1 [, item2 ... [, itemN ]]) . See "Pushing and Popping" in this chapter.

reverse

 

x

x

x

 

x

x

x

x

x

 

Returns: Array object

 

This method returns the array with the order of the elements reversed .

 

Syntax: array .reverse() .

shift

   

x

x

       

x

x

 

Returns: Array entry

 

This method removes the first element of an array and returns that element.

 

Syntax: array .shift() .

slice

   

x

x

   

x

x

x

x

 

Returns: Array object

 

This method returns a slice, or section, of an array.

 

Syntax: array .slice( start , end ) ; where start is the beginning index of the slice, and end is the ending index of the slice.

sort

 

x

x

x

 

x

x

x

x

x

 

Returns: Array object

 

This method sorts an array.

 

Syntax: array .sort([ function ]) ; where function is a function that returns a negative value if the first argument passed to it is less than the second argument (according to the way you want to sort the items), zero if the two arguments are equal, and a positive value if the first argument is greater than the second argument. If you omit function , the array is sorted in ascending text order. See "Sorting Arrays" in this chapter.

splice

   

x

x

       

x

x

 

Returns: Array object

 

This method removes elements from an array and can also insert new elements. This method returns the deleted elements.

 

Syntax: array .splice( start , count [, item1 [, item2 [, ... [, itemN ]]]]) ; where start is the location at which to start deleting elements, count is the number of elements to delete, and item1 to itemN are elements to insert to replace the deleted elements.

toLocaleString

     

x

       

x

x

 

Returns: String

 

This method returns the array in locale-sensitive string form.

 

Syntax: array .toLocaleString() .

toString

 

x

x

x

 

x

x

x

x

x

 

Returns: String

 

This method returns the array in string form.

 

Syntax: array .toString() .

unshift

   

x

x

       

x

x

 

Returns: Array entry

 

This method returns the array with the given elements inserted at the beginning.

 

Syntax: array .unshift(([ item1 [, item2 [, ... [, itemN ]]]]) , where item1 to itemN are elements to insert at the start of the array.



Inside Javascript
Inside JavaScript
ISBN: 0735712859
EAN: 2147483647
Year: 2005
Pages: 492
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net