Visit Mozilla.org

Core JavaScript 1.5 Reference:Global Objects:Array:prototype

From MDC


Contents

[edit] Summary

Represents the prototype for the Array constructor.

[edit] Description

Array instances inherit from Array.prototype. As with all constructors, you can change the constructor's prototype object to make changes to all Array instances.

[edit] Properties

constructor
Specifies the function that creates an object's prototype.
index
This is pseudo-property of Array prototypes because it is not inherited by default. It is, in fact, only present in arrays created by regular expression matches. The property represents the zero-based index of the match in the string.
input
This property is only present in arrays created by regular expression matches. It reflects the original string against which the regular expression was matched.
length
Reflects the number of elements in an array.

[edit] Methods

[edit] Mutator methods

These methods modify the array:

pop
Removes the last element from an array and returns that element.
push
Adds one or more elements to the end of an array and returns the new length of the array.
reverse
Reverses the order of the elements of an array -- the first becomes the last, and the last becomes the first.
shift
Removes the first element from an array and returns that element.
sort
Sorts the elements of an array.
splice
Adds and/or removes elements from an array.
unshift
Adds one or more elements to the front of an array and returns the new length of the array.

[edit] Accessor methods

These methods do not modify the array and return some representation of the array.

concat
Returns a new array comprised of this array joined with other array(s) and/or value(s).
join
Joins all elements of an array into a string.
slice
Extracts a section of an array and returns a new array.
toSource
Non-standard
Returns an array literal representing the specified array; you can use this value to create a new array. Overrides the Object.prototype.toSource method.
toString
Returns a string representing the array and its elements. Overrides the Object.prototype.toString method.

Introduced in JavaScript 1.6


indexOf
Returns the first (least) index of an element within the array equal to the specified value, or -1 if none is found.
lastIndexOf
Returns the last (greatest) index of an element within the array equal to the specified value, or -1 if none is found.

[edit] Iteration methods

Introduced in JavaScript 1.6


Several methods take as arguments functions to be called back while processing the array. When these methods are called, the length of the array is sampled, and any element added beyond this length from within the callback is not visited. Other changes to the array (setting the value of or deleting an element) may affect the results of the operation if the method visits the changed element afterwards. While the specific behavior of these methods in such cases is well-defined, you should not rely upon it so as not to confuse others who might read your code. If you must mutate the array, copy into a new array instead.

filter
Creates a new array with all of the elements of this array for which the provided filtering function returns true.
forEach
Calls a function for each element in the array.
every
Returns true if every element in this array satisfies the provided testing function.
map
Creates a new array with the results of calling a provided function on every element in this array.
some
Returns true if at least one element in this array satisfies the provided testing function.

Introduced in JavaScript 1.8


reduce
Apply a function simultaneously against two values of the array (from left-to-right) as to reduce it to a single value.
reduceRight
Apply a function simultaneously against two values of the array (from right-to-left) as to reduce it to a single value.

[edit] Generic methods

Many methods on the JavaScript Array object are designed to be generally applied to all objects which "look like" Arrays. That is, they can be used on any object which has a length property, and which can usefully be accessed using numeric property names (as with array[5] indexing). Some methods, such as join, only read the length and numeric properties of the object they are called on. Others, like reverse, require that the object's numeric properties and length be mutable; these methods can therefore not be called on objects like String, which does not permit its length property or synthesized numeric properties to be set.

Introduced in JavaScript 1.6

Methods inherited from Object.prototype
__defineGetter__, __defineSetter__, hasOwnProperty, isPrototypeOf, __lookupGetter__, __lookupSetter__, __noSuchMethod__, propertyIsEnumerable, unwatch, valueOf, watch

[edit] See also