Although the key of this collection can only be of String type, unlike various collection classes in Java that can use various objects as Key, it is enough for realizing general client JS functions. Similarly, because all JS internal objects inherit from the Object object, the Array object of JS can actually use strings as array subscripts, just like array variables in PHP. From Bird Food Xuan.
Array is an internal object provided by JavaScript. It is a standard collection. We can add (push) and delete (shift) the elements in it. We can also traverse the elements in it through a for loop. In addition to the array Can we have other collections in JavaScript?
Due to the language features of JavaScript, we can dynamically add and remove properties to universal objects. So Object can also be regarded as a special collection of JS. Let’s compare the characteristics of Array and Object:
Array:
New: var ary = new Array(); or var ary = [];
Add: ary.push(value );
Delete: delete ary[n];
Traverse: for ( var i=0 ; i < ary.length ; i ) ary[i];
Object:
New: var obj = new Object(); or var obj = {};
Add: obj[key] = value; (key is string)
Delete: delete obj[key];
Traversal: for ( var key in obj ) obj[key];
From the above comparison, we can see that Object can be used as a collection. Use the Popup window to create an infinite web page menu (3) I introduced the __MenuCache__ implemented by Eric, which is also a simulated collection object.
If we want to retrieve a specified value in Array, we need to iterate through the entire array: