For JavaScript, its own array object is only a number of arrays, can not provide through the keyword to obtain the saved data, jquery source provides a very good way to solve the problem, first look at the source code:
Copy Code code as follows:
function Createcache () {
var keys = [];
function cache (key, value) {
Use (key + "") to avoid collision with native prototype
Properties (Issue #157)
if (keys.push (key = = "") > Expr.cachelength) {
Only keep the most recent entries
Delete 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 calls the following way:
Copy Code code as follows:
var codecache = Createcache ();
Source code, the keys used to save the key, and the cache object to save the key value pairs, and through the global variable expr.cachelength the maximum number of control keys, if more than that number, then automatically delete the first key and key value pairs.
This code makes use of the closure structure so that the 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 modify the cache property so that the key and key value pairs do not match. However, as long as not deliberately spoof, this itself should not have much relationship.
Of course, it also cannot swear a perfect dictionary object, because it does not provide key functions such as duplicate judgment of the primary key, and interested friends can refine it.