Array.prototype

摘要

介绍 Array 构造函数的prototype(原型)。

描述

Array 实例由 Array.prototype继承而来。 正如所有的构造函数一样,你可以通过改造构造函数的原型对象来改变所有的 Array 实例。

属性

constructor
创建对象的原型的函数。
length
数组中元素的个数。

方法

变值方法

这些方法会改变数组本身:

pop
移除数组的最后一个元素,并返回该元素。
push
在数组末尾插入一个或多个元素,并返回新数组的长度。
reverse
颠倒数组元素的顺序 -- 第一个元素变成最后一个,最后一个变成第一个。
shift
移除数组的第一个元素,并返回该元素。
sort
对数组的元素进行排序,并返回数组。
splice
添加/删除数组中的一个或者多个元素。
unshift
在数组的第一个位置添加一个或者多个元素,并返回数组的新长度。

存取方法

这方法不会改变数组本身,它会返回数组的副本。

concat
合并其它数组或者值,并返回新数组。
join
将数组的所有元素放在一个字符串中。
slice
提取数组的片断,并返回新数组。
 
toSource
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.
indexOf Requires JavaScript 1.6
Returns the first (least) index of an element within the array equal to the specified value, or -1 if none is found.
lastIndexOf Requires JavaScript 1.6
Returns the last (greatest) index of an element within the array equal to the specified value, or -1 if none is found.

迭代方法

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.

forEach Requires JavaScript 1.6
Calls a function for each element in the array.
every Requires JavaScript 1.6
Returns true if every element in this array satisfies the provided testing function.
some Requires JavaScript 1.6
Returns true if at least one element in this array satisfies the provided testing function.
filter Requires JavaScript 1.6
Creates a new array with all of the elements of this array for which the provided filtering function returns true.
map Requires JavaScript 1.6
Creates a new array with the results of calling a provided function on every element in this array.
reduce Requires JavaScript 1.8
Apply a function against an accumulator and each value of the array (from left-to-right) as to reduce it to a single value.
reduceRight Requires JavaScript 1.8
Apply a function against an accumulator and each value of the array (from right-to-left) as to reduce it to a single value.

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). TODO: give examples with Array.prototype.forEach.call, and adding the method to an object like JavaArray or String. 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.

JavaScript 1.6 引入

See also

Document Tags and Contributors

Contributors to this page: sleepholic, andy12530
Last updated by: andy12530,