JavaScript應用資料緩衝對象申明__Java

來源:互聯網
上載者:User

JavaScript應用資料緩衝對象申明,做到無資料時自動調用對應函數擷取資料並緩衝,當再次擷取資料時,直接從緩衝中擷取。

(function(){    var cacheObject={};    Object.defineProperty(window,'AppDataCache',{    writable: false,    enumerable: false,    configurable: false,    value: {}    });    Object.defineProperties(AppDataCache,{        add: {        writable: false,        enumerable: false,        configurable: false,        value: function(key, data_fn){            debugger;            var obj = {};            if(typeof(key)==='string'){                obj[key]=data_fn;            } else if(typeof(data_fn)==='string') {                obj[data_fn]=key;            } else if(key && typeof(key)==='object') {                obj=key;            }            for(var k in obj) {                if(k=='add' || k=='clear')throw Error("add key name can't equals [add,clear]");                if(cacheObject.hasOwnProperty(k) && console && console.warn) {                    delete cacheObject[k];                    console.warn("Already exist["+k+"],Cover this key data");                }                (function(k_,data_){                    Object.defineProperty(AppDataCache,k_,{                        //writable: true,                        enumerable: true,                        configurable: true,                        get: function(){                            if(!cacheObject[k_]){                                cacheObject[k_]=typeof(data_)==='function'?data_():data_;                            }                            return cacheObject[k_];                        }                    });                })(k,obj[k])            }        }      },      clear: {        writable: false,        enumerable: false,        configurable: false,        value: function(key){            if(key) {                return delete cacheObject[key];            }            for(var k in cacheObject){                delete cacheObject[k];            }            return true;        }      }    });})()

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.