imitating jquery, a cache system is designed. Similar to jquery. Data, prototype and mootools are also used to assist the Event System and cache the generated data, rather than caching the results of the previous calculation of common functions. Prototype uses its hash class. mootools does not take a closer look, and it seems to be used to cache UUID internally. One consensus is that it is very useful to set UUID for the elements used on the page. When searching for elements, you can avoid repeated searches or bind them to the Event Callback Function. UUID is currently only supported by IE. It is called uniqueid, in the format of MS _ ID \ D +. The following numbers are also named uniquenumber. Jquery is a uniquenumber, and its cache system is very complex. It supports caching a single data (using the data read/write method) and a group of data (using queue to delete dequeue ). There is no way, because it is a self-built system and does not share the responsibility of using a custom data type like prototype. It's time to get started and talk about my cache system. It uses my super array object to implement functions such as queue and dequeue. However, my super array objects can do more, such as filter, foreach, MAP, reduce, one, toobject, contains, and remove.
Dom. eventtypes = Dom. array (string ("Abort blur change click contextmenu \ dblclick error focus keydown keypress keyup load mousedown \ mouseenter mouseup mouseleave mousemove Mouseover mouseout \ reset resize select submit unload "). match (/\ W +/g )); ****** * ***************** Dom. mixin ({UUID: 0, storage :{}, buildcache: function (item) {var key, cache = Dom. storage; // such If window if (item. setinterval & (item! = Window &&! Item. frameelement) {key = "dom-window"} else if (item. nodetype) {If (! Item. UUID) {item. UUID = "dom" + DOM. UUID ++ Dom. cache ("UUID", "UUID-set", item); // Save the element reference} // if the current element does not have the uuid attribute, then add a key = item for it. UUID} else if (Dom. isstring (item) {key = item;} else {Throw "item must be element node, window or string"} If (! Cache [Key]) {cache [Key] ={};} return cache [Key]}, cache: function (item, name, data) {// read/write cache var cache = Dom. buildcache (item); If (Data! = Undefined) {If (Dom. isfunction (data) {// cache function, set a uuid in the function, generate the tostring () method, and remove the blank var UUID = (Data + ""). replace (/\ s +/g, ''); data. UUID = UUID;} If (! Cache [name]) {// set and list allow the caching of a group of data. // If the-set suffix is included, if (/-set $ /. test (name) {// if it is the first time to store cache [name] = array (data); cache [name]. type = "set";} else if (/-list $ /. test (name) {// cache [name] = array (data); cache [name]. type = "list";} else {cache [name] = data;} else {var type = cache [name]. type; If (type & type = "set") {cache [name]. ensure (data)} else if (type & type = "list") {Cache [Name]. push (data)} else {cache [name] = data ;}} return item ;}else {return cache [name] }}, // The parameter can be 1, 2, 3 removecache: function (item, name, data) {If (! Arguments. Length) return; var cache = Dom. buildcache (item), isdom = !! Item. nodetype; If (isdom & Dom. eventtypes. contains (name) {name = Name + "-handlers-list" ;}; if (arguments. length = 3) {// remove the specified data if (Cache [name] instanceof Dom. array & cache [name]. length> 0) {// for a super array composed of event-bound functions, when caching it, we set a uuid for each function. // now we compare whether the uuid of the passed function is equal to the uuid of an element in the super array. // If yes, we will get its reference, with it, we can use the remove method to remove it. If (Dom. isfunction (data) {var UUID = (Data + ""). replace (/\ s +/g, ''), FN = cache [name]. one (function (FN) {return fn. UUID = UUID;}); If (FN) {cache [name]. remove (FN) ;}} else {cache [name]. remove (data);} If (Cache [name]. length = 0) Dom. removecache (item, name);} else {Dom. removecache (item, name) ;}} else if (arguments. length = 2) {// remove the entry Delete cache [name] If (! Dom. isextensible (cache) {Dom. removecache (item) ;}} else {Delete cache; If (isdom) {Dom. removecache ("UUID", "UUID-set", item); Dom. removeattr (item, "UUID") ;}}, using Ache: function () {var cache = Dom. buildcache ("UUID", "UUID-set"); If (! Cache) return; // return the cache directly if it is null. foreach (function (EL) {Dom. removeattr (El, "UUID") ;}); Dom. storage = NULL ;}});
Buildcache is used in Dom. a namespace is opened on storage. cache is used to store various data in the namespace. It can be a single data, a group of data, and a group of data can be divided into list and set, the difference is whether repeated elements are allowed. Repeated lists are more important than jquery. array set by queue. For the processing of window objects, jquery creates a windowdata in the closure, and it opens a space named Dom-window in Dom. Storage. Removecache is equivalent to jquery. removedata and jquery. unquere, but it is also powerful and can be used based on the number of input parameters. Revoke ache directly deletes all caches.
It is worth noting that setting private attributes for DOM elements in IE will cause memory leakage, and all of them need to be manually removed during page unload. This is also done for various major libraries. Dom. cache ("UUID", "UUID-set", item) is used to retrieve all elements that have been set UUID during unload and remove their UUID one by one. However, you can call the sort ache method.