Data caching for the "JavaScript" Closure application

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.