理顺 JavaScript (11) - 数组
2010-09-14 13:40:47 来源:WEB开发网数组直接量
var arr;
arr = ['AA', 'BB', 'CC'];
alert(arr.toLocaleString()); //AA, BB, CC
arr = [11, 22, 33];
alert(arr.toLocaleString()); //11.00, 22.00, 33.00
/* 数组元素可以是任意类型, 包括数组本身 */
arr = ['AA', 'BB', 123];
alert(arr.toLocaleString()); //AA, BB, 123.00
alert(arr[0]); //AA
alert(arr[1]); //BB
alert(arr[2]); //123
/* 可先站定维数, 以后赋值 */
arr = [ , , ];
arr[0] = 11;
arr[1] = 22;
arr[2] = 33;
alert(arr.toLocaleString()); //11.00, 22.00, 33.00
/* 先给个空数组, 再指定维数 */
arr = [];
alert(arr.length); //0
arr.length = 3;
arr[0] = 11;
arr[1] = 22;
arr[2] = 33;
alert(arr.toLocaleString()); //11.00, 22.00, 33.00
/* 可用 length 属性改变数组的大小 */
arr = [1,2,3,4,5,6];
arr.length = 2;
alert(arr.toLocaleString()); //1.00, 2.00
数组对象的三种建立方法
var arr;
arr = new Array(11, '22', true);
alert(arr.toLocaleString()); //11.00, 22, true
arr = new Array(3);
arr[0] = 11;
arr[1] = '22';
arr[2] = true;
alert(arr.toLocaleString()); //11.00, 22, true
arr = new Array();
arr.length = 3;
arr[0] = 11;
arr[1] = '22';
arr[2] = true;
alert(arr.toLocaleString()); //11.00, 22, true
Tags:理顺 JavaScript 数组
编辑录入:爽爽 [复制链接] [打 印]更多精彩
赞助商链接