簡體   English   中英

從本地存儲中檢索數據時如何將數據轉換為不同類型

[英]How to convert the data into different types when retrieving from local storage

我正在寫一個小庫來處理localStorage中的數據。

下面是代碼片段:

function AppStorage (appName) {
    "use strict";

    var prefix  = appName;

    var saveItem = function (key, value) {

        if (!key || !value) {

            console.error("Missing parameters \"key\" and \"value\"");    

            return false;
        }

        if (window.localStorage && window['localStorage']) {

            try {
                if (typeof value === 'Object') localStorage.setItem(prefix + '_' + key, JSON.stringify(value));
                if (typeof value === 'string') localStorage.setItem(prefix + '_' + key, value);

                return true;

            } catch (e) {

                return false;

            }

        } else  {

            return false;

        }

    }

    var getItem = function (key) {

        if (!key) {

            console.error("Missing parameter \"key\"");    

            return false;
        }

        if (window.localStorage && window['localStorage']) {
            try {

                return localStorage.getItem(prefix + '_' + key);                

            } catch (e) {

                return false;

            }

        } else  {

            return false;

        }

    }

    var removeItem = function (key) {

        if (!key) {

            console.error("Missing parameter \"key\"");

            return false;
        }

        if (window.localStorage && window['localStorage']) {

            try {
                localStorage.removeItem(prefix + '_' + key);
                return true;

            } catch (e) {
                return false;
            }

        } else  {

            console.log("Browser does not support HTML5 Web Storage");

        }

    }

    return {
        set: function (key, value) {
            return saveItem(key, value);
        },
        get: function (key) {
            return getItem(key);
        },
        remove: function (key) {
            return removeItem(key);
        }
    }

}

var as = new AppStorage ('MyApp');

我如何堅持以下兩個問題。

1)通過get()檢索數據時,存儲的信息以string形式返回。 在傳遞之前,我需要以相同的格式接收此信息。

2)是否可以進一步完善以下代碼段。

希望有幫助!

 var ClientStorageService = (function(persistent, session) { if(!persistent && !session) { throw new Error('ClientStorage Not Suppoerted'); } function ClientStorage() {}; ClientStorage.prototype.driver = persistent || session; ClientStorage.prototype.prefix = 'HELLOWORLD_'; ClientStorage.prototype.usePersistent = function() { if(persistent) { this.driver = persistent; return true; } return false; }; ClientStorage.prototype.useSession = function() { if(session) { this.driver = session; return true; } return false; }; ClientStorage.prototype.read = function(key) { key = this.prefix + key; var data = this.driver.getItem(key); if(data) { try { data = JSON.parse(data); } catch(e) {} return data; } return null; }; ClientStorage.prototype.store = function(key, val) { key = this.prefix + key.toString(); try { val = JSON.stringify(val); } catch(e) {} this.driver.setItem(key, val); return this; }; ClientStorage.prototype.clear = function(key) { var task = 'clear'; if(key) { key = this.prefix + key; task = 'removeItem'; } this.driver[task](key); }; return new ClientStorage(); })(window.localStorage, window.sessionStorage); ClientStorageService.store('FOO', { greetings: 'Hello World' }); var result = ClientStorageService.read('FOO'); console.log('result is', result); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM