1声明方式:
var arr = [元素,元素,元素。。。];
var arr = new Array(元素,元素,元素。。。);
var arr = new Array(3);
arr[0] = 元素;
arr[1] = 元素;
2 举例
//③ 声明
var food = new Array(3);
//给数组丰富元素
food[0] = "bread";
food[1] = "drink";
food[2] = "milk";
food['yuanxiao'] = "元宵";
console.log(food);
//对象访问成员:对象[成员名] 或 对象.成员名
//数组访问元素:数组[下标]
//console.log(food.2);
console.log(food.yuanxiao); //元宵
console.log(food['yuanxiao']);//元宵
3数组和对象的比遍历
<script type="text/javascript">
var color = ['red','blue','green'];
color['jinse'] = "gold";
color[10] = "pink";
//① for循环遍历,为了遍历效果更好,数组下边最好是0/1/2...规则连续的
//for(var i=0; i<color.length; i++){
// console.log(i+'--'+color[i]);
//}
//② for-in,数组 和 对象都可以遍历
//for (var 下标变量 in 数组)
for(var k in color){
console.log(k+'=='+color[k]);
}
</script>