For JavaScript, its own array object is only a number of arrays, can not provide through the keyword to get saved data, jquery source provides a very good way to solve this problem, first look at the source code:
function Createcache () {var keys = [];function cache (key, value) {//Use (key + "") to avoid collision with native Protot ype//properties (see Issue #157) if (keys.push (key + = ") > Expr.cachelength) {//Only keep the most recent Entriesdel Ete Cache[keys.shift ()];} Return (Cache[key] = value);} return cache;}
The above source is to create a compilation of the results of the cache, the code is called the following way:
var codecache = Createcache ();
In the source code, keys is used to hold the key, and the cache object is used to hold the key value pair, and the maximum number of control keys is expr.cachelength by the global variable, and the first key and the key value pair are automatically deleted if the number is exceeded.
This code leverages the structure of the closure so that external code cannot access the keys variable, which ensures the security of the keys variable. Of course, because of the characteristics of JavaScript statements, external code can also make the key and key-value pairs do not match by modifying the cache property. However, as long as not intentionally spoof, this itself should not have too much relationship.
Of course, it is also not possible to pledge a perfect Dictionary object, because it does not provide key functions such as primary key repetition judgment, and interested friends can perfect it.
How JavaScript creates Dictionary objects