<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>數組與對象</title>
<script type="text/javascript">
//創建數組
var arr = new Array();
arr[0] = 123;
arr[1] = "Hello";
arr[2] = 12.23;
arr[3] = true;
arr[6] = 45;
alert(arr[0]);//輸出123
alert(arr.length);//輸出4
alert(arr[4]);//輸出undefined
alert(arr[arr.length-1]);//輸出最後一個元素,true
/*遍歷數組*/
for(var i=0;i<arr.length && arr[i] != undefined;i++)
{
alert("數組第"+i+"元素:"+arr[i]);
}
//創建數組並賦值
var b = new Array(1,2,3,4,5,6);
//創建一個長度爲0的數組
var d = [];
alert(d,length);//輸出爲0
//創建數組
var date = new Array();
date[0] = 1234;
date[1] = "H";
date[2] = true;
date[3] = 120.12;
//向數組末尾添加元素
date.push(25);
alert(date[date.length-1]);//輸出25
date.push(1,2,3,4);
alert(date.length);//輸出9
//delete刪除元素,數組長度不變
delete date[0];
alert(date[0]);//輸出undefined
alert(date.length);//輸出9
alert(date.join("#"));//輸出#H#true#120.12#25#1#2#3#4
</script>
</head>
<body>
</body>
</html>
JavaScript對象和數組
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.