In JSArrayIt's a baby, not only an array, but also a Dictionary, but also a Stack.
-
var pinyins = new Array();
-
pinyins["people"] = "ren";
-
pinyins["mouth"] = "kou";
-
pinyins["hand"] = "shou";
-
alert(pinyins["people"]);
-
alert(pinyins.man);
Use it like Hashtable and Dictionary, and it is as efficient as them.
In JS, Array: DynamicArray, special dictionaries can be used as dictionaries. The index is the key.
In JS, as long as you have Array, you will have arrays, Lists, and Hashtables at the same time.
-
var arr = new Array();
-
arr[0] = "tom";
-
arr[1] = "jim";
-
for (var i in arr) {//The printed out is 0, 1, 2...proves that array usage is just a special case of Dictionary usage.
-
alert(i+arr[i]);
-
}
-
//dictionary
-
var dic = new Array();
-
dic["zs"] = "Zhang San";
-
dic["ls"] = "Li Si";
-
dic["ww"] = "Wang Wu";
-
//The usage in this is wrong, it cannot be used here
-
for (var i = 0; i < dic.length; i++) {
-
alert(dic[i]);
-
}
-
//It's OK to write this
-
alert(dic["zs"]);
-
alert(dic.zs);
-
for (var d in dic) { //There is no foreach in JavaScript, but you can use for as foreach and traverse it.
-
alert(d);
-
alert(dic[d]);
-
}
-
var arr = ["tom", "jim", "lily"];
-
alert(arr); //Implicitly call JavaScript's built-in function join, so this line is the same as the following line.
-
alert(arr.join(",")); //The effect is the same as the previous line.