JavaScript Array

来源:互联网 发布:自制短片软件 编辑:程序博客网 时间:2024/04/29 14:37

 

JavaScript provides both a long way and a shortcut to generate and populate an array. The long way is to use the Array object constructor. If you specify no parameters for the constructor, you create an empty array, which you may then populate with data entry by entry:

var myArray = new Array( );myArray[0] = "Alice";myArray[1] = "Fred";...

You do not have to declare a fixed size for an array when you create it, but you may do so if you wish, by passing a single integer value as a parameter to the constructor method:

var myArray = new Array(12);

This creates an array of 12 entries whose values are null.

If you supply more than one comma-delimited parameter to the constructor method, the arguments are treated as data for the array entries. Thus, the following statement:

var myArray = new Array("Alice", "Fred", "Jean");

creates a three-item array, each item containing a string value.

A shortcut approach to the same action lets you use square brackets to symbolize the array constructor:

var myArray = ["Alice", "Fred", "Jean"];

You can use the shortcut syntax in IE 4 or later and NN 4 or later.

 

Array 对象的方法

FF: Firefox, N: Netscape, IE: Internet Explorer

方法描述FFNIEconcat()连接两个或更多的数组,并返回结果。144join()把数组的所有元素放入一个字符串。元素通过指定的分隔符进行分隔。134pop()删除并返回数组的最后一个元素。(模拟Stack)145.5push()向数组的末尾添加一个或更多元素,并返回新的长度。(模拟Stack,Queue)145.5reverse()颠倒数组中元素的顺序。134shift()删除并返回数组的第一个元素。(模拟Queue)145.5slice()从某个已有的数组返回选定的元素144sort()对数组的元素进行排序。134splice()删除元素,并向数组添加新元素。145.5toSource()代表对象的源代码14-toString()把数组转换为字符串,并返回结果。134toLocaleString()把数组转换为本地数组,并返回结果。134unshift()向数组的开头添加一个或更多元素,并返回新的长度。146valueOf()返回数组对象的原始值。124

Array 对象的属性

FF: Firefox, N: Netscape, IE: Internet Explorer

属性描述FFNIEconstructor对创建此对象的函数的一个引用124index 134input 134length设置或返回数组中元素的数目。124prototype使您有能力向对象添加属性和方法124
 
 
Note: 利用Array对象可模拟Stack和Queue行为。