数组下标可以为任意表达式,甚至为任意数据类型:
1、数字作为下标
2、字符串作为下标
1)数组本身也是对象,这些字符串会被结实为对象的属性,数组本身的length不会发生变化
2)与对象的类同结构
var arr1 = new Array(); arr1['name'] = "xhaja"; arr1['age'] = 34 for(var key in arr1) { console.log(key) //name , age console.log(arr1[key]) // xhaja , 34 } console.log(arr1.length) // 结果为0
var arr = [] arr["1"] = 100; console.log(arr["1"]) //100 console.log(arr[1]) //100 arr[true] = 100; console.log(arr[true]) //100 arr[-1] = 100; console.log(arr["-1"]) //100 console.log(arr[-1]) //100
应用:哈希表