Many events in the recent development are triggered frequently, and the same data is called in the background every time the event is triggered because there is no cache processing. These days I suddenly realized that my code has a lot of space to optimize, and then think of the closure can have the function of caching, so it was deeply researched to facilitate the optimization of the previously written code. After completing the optimization, write the following example as a summary:
var a = (function B () {var _cache = {};return {dosometing:function (name) {if (name in cache) {var data = _cache[name];/ /get Cache data dosometingbydata;} else {_cache[name] = Dosometingtogetdata (name);}},};}) (); function Dosometingtogetdata () {//can fetch data in the background here, or it can be another operation, which is to get the data you need to cache and return it;}
var a = (function B () {var _cache = {};return {setcache:function (name) {_cache[name] = Dosometingtogetdata (name);},get Cache:function (name) {return _cache[name];}}); var c = function () {A.setcache (' daihere '); var data = A.getcache (' Daihere ');}
Data caching for the "JavaScript" Closure application