亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

localStorage和sessionStorage

RancherLabs / 2004人閱讀

摘要:為本地存儲(chǔ),是一種永久性存儲(chǔ),除非手動(dòng)清除。是會(huì)話存儲(chǔ),當(dāng)瀏覽器關(guān)閉后,存儲(chǔ)數(shù)據(jù)會(huì)自動(dòng)清除。無需轉(zhuǎn)換數(shù)據(jù)類型獲取存儲(chǔ)的數(shù)據(jù),無需指定鍵值。移除數(shù)據(jù)清空所有的存儲(chǔ)清空所有的存儲(chǔ)

localStoage為本地存儲(chǔ),是一種永久性存儲(chǔ),除非手動(dòng)清除。sessionStorage是會(huì)話存儲(chǔ),當(dāng)瀏覽器關(guān)閉后,存儲(chǔ)數(shù)據(jù)會(huì)自動(dòng)清除。兩種存儲(chǔ)方式幾乎都有相同的方法和屬性;

1.localStorage.length 和 sessionStorage.length : 返回本地存儲(chǔ)列表長(zhǎng)度;需要說明的是可以通過localStorage對(duì)象獲取對(duì)應(yīng)的存儲(chǔ)數(shù)據(jù),比如已經(jīng)存儲(chǔ)了一個(gè)鍵為name的數(shù)據(jù),使用localStorage.name獲取存儲(chǔ)數(shù)據(jù);下面的方法都是基于localStorage 對(duì)象 或 sessionStorage 對(duì)象;

2.getItem( keyName )方法:通過鍵獲取存儲(chǔ)的數(shù)據(jù);

3.setItem( keyName,value )方法:存儲(chǔ)數(shù)據(jù),keyName為存儲(chǔ)數(shù)據(jù)的鍵,value實(shí)際要存儲(chǔ)的數(shù)據(jù)(可以把keyName理解成 給要存儲(chǔ)的數(shù)據(jù)起的名字,便于管理);

4.removeItem( keyName )方法: 將指定鍵名的數(shù)據(jù)刪除;

5.clear() 方法:清空所有的存儲(chǔ)數(shù)據(jù);

6.key() 方法: 獲取鍵值,需要注意的是并不能返回指定布爾值需要作進(jìn)一步處理,下面是針對(duì)兩種不同的存儲(chǔ)方式使用統(tǒng)一的函數(shù)方式實(shí)現(xiàn),歡迎各位提出意見。

7.es6 版本 發(fā)布在github 也可以使用 npm i -S feitools

https://github.com/lizhanyi/t...
    /*  功能:拋出異常 , 調(diào)試使用
        @param msg , 異常信息
    */
    throwError : function( msg ){ // 調(diào)試使用
        throw new Error( msg );
    }


    /*  功能:設(shè)置和獲取數(shù)據(jù)
        @param key,存儲(chǔ)的鍵
        @param value,待存儲(chǔ)的數(shù)據(jù)
        @param boolean,區(qū)分對(duì)象sessionStorage和localStorage
    */
    setItem : function( key, value/*, boolean*/ ){
        var 
        args = arguments,
        len = args.length,
        storage;
        len == 0 && this.throwError("Not enough arguments to Storage.getItem.");
        if( args[len-1] == true ){// 是session
            storage = window.sessionStorage;
            len == 1 && this.throwError("Not enough arguments to Storage.getItem.");
            len >= 3 && storage.setItem( args[0], args[1] );
            if( len == 2 ) return storage.getItem( args[0] );
        }else{// 是local
            storage = window.localStorage;
            len >= 2 && storage.setItem( args[0], args[1] );
            if( len == 1 ) return storage.getItem( args[0] );

        }

    }



    /*  功能:刪除存儲(chǔ)數(shù)據(jù)
        @param key,待刪除的數(shù)據(jù)的鍵
        @param boolean,區(qū)分對(duì)象sessionStorage和localStorage
    */
    removeItem : function( key, boolean){ // 刪除指定 key 的存儲(chǔ)數(shù)據(jù)
        var storage;
        boolean ? 
            storage = window.sessionStorage : storage = window.localStorage;
        return storage.removeItem( key );
    }


    clearItem : function(/*boolean*/){// 清空所有存儲(chǔ)
        arguments[0] ?  window.sessionStorage.clear() : window.localStorage.clear();
    }


    /*  功能:判斷是否有某一鍵名的數(shù)據(jù),若存儲(chǔ)數(shù)據(jù)存在返回true,否則返回false
        @param key,指定的鍵名
        @param boolean,區(qū)分對(duì)象sessionStorage和localStorage
        用這種方式判斷是否存儲(chǔ)了某個(gè)數(shù)據(jù),應(yīng)該會(huì)好于使用getItem()方法;
    */
    getKey : function( keyName/*boolean*/){ // 獲取數(shù)據(jù)存儲(chǔ)的鍵
        var 
        temp,
        args = arguments;
        len = args.length;
        args[len-1] == true ?  temp = window.sessionStorage : temp = window.localStorage;
        for(var i = 0; i < temp.length; i++)
            if( keyName == temp.key(i) ) 
                return true;
        return false;
    }

上面的封裝在使用的時(shí)候有點(diǎn)糾結(jié),使用多次和時(shí)間長(zhǎng)的時(shí)候自己也不知道是使用了seeeion還是使用了local,所謂實(shí)踐是檢驗(yàn)真理的唯一標(biāo)準(zhǔn)

優(yōu)化一下代碼,使用構(gòu)造函數(shù)方式實(shí)現(xiàn)
/*
    @param way,存儲(chǔ)方式,指定是session或local存儲(chǔ)
*/
function Storage( way ){
    this.map = {
        "session" : window.sessionStorage,
        "local" : window.localStorage
    },
    this.getItem = function( key ){
        return this.map[way].getItem( key );
    },
    this.setItem = function( key, value ){
        this.map[way].setItem( key,value )
    },
    this.removeItem = function( key ){
        this.map[way].removeItem( key );
    },
    this.clear = function(){
        this.map[way].clear();
    },
    this.getKey = function( key ){
        //var len = map.way.length;
        return key in map[way];
    }
};

var local = new Storage("local");// 創(chuàng)建一個(gè)本地存儲(chǔ)的實(shí)例
    local.setItem("key","data");// 存儲(chǔ)數(shù)據(jù)
    local.getItem("key"); // 獲取本地存儲(chǔ)數(shù)據(jù)
    local.removeItem("key"); // 刪除鍵名為key的存儲(chǔ)數(shù)據(jù)
    local.getKey("key"); // 判斷是否存在某一鍵的數(shù)據(jù)
    local.clear();// 清空本地存儲(chǔ)(刪除所有的本地存儲(chǔ)的數(shù)據(jù))
    
var session = new Storage("session");// 創(chuàng)建一個(gè)session存儲(chǔ)的實(shí)例
    session.setItem("key","data");// 存儲(chǔ)數(shù)據(jù)
    session.getItem("key"); // 獲取session存儲(chǔ)數(shù)據(jù)
    session.removeItem("key"); // 刪除鍵名為key的存儲(chǔ)數(shù)據(jù)
    session.getKey("key"); // 判斷是否存在某一鍵的數(shù)據(jù)
    session.clear();// 清空會(huì)話存儲(chǔ)(刪除所有的session存儲(chǔ)的數(shù)據(jù))
繼續(xù)優(yōu)化:(使用原型對(duì)象,共享屬性和方法)
function Storage( way ){
    this.way = way;
};
Storage.prototype.map = {
    "session" : window.sessionStorage,
    "local" : window.localStorage
};
Storage.prototype.setItem = function( key, value){
    this.map[this.way].setItem( key, value )
};
Storage.prototype.getItem = function( key ){
    return this.map[this.way].getItem( key );
};
Storage.prototype.removeItem = function( key ){
    this.map[this.way].removeItem( key )
};
Storage.prototype.clear = function(){
    this.map[this.way].clear();
};
Storage.prototype.getKey = function( key ){
    return key in this.map[this.way];
};

var local = new Storage("local");// 創(chuàng)建一個(gè)本地存儲(chǔ)的實(shí)例
    local.setItem("key","data");// 存儲(chǔ)數(shù)據(jù)
    local.getItem("key"); // 獲取本地存儲(chǔ)數(shù)據(jù)
    local.removeItem("key"); // 刪除鍵名為key的存儲(chǔ)數(shù)據(jù)
    local.getKey("key"); // 判斷是否存在某一鍵的數(shù)據(jù)
    local.clear();// 清空本地存儲(chǔ)(刪除所有的本地存儲(chǔ)的數(shù)據(jù))
    
再次優(yōu)化代碼:(針對(duì)上面的寫法再來一次簡(jiǎn)化)
function Storage( way ){
    this.way = way;
};
Storage.prototype = {
    map : {
        "session" : window.sessionStorage,
        "local" : window.localStorage
    },
    setItem : function( key, value ){
        this.map[this.way].setItem( key, value )
    },
    getItem : function( key ){
        return this.map[this.way].getItem( key );
    },
    removeItem : function( key ){
        this.map[this.way].removeItem( key );
    },
    clear : function(){
        this.map[this.way].clear();
    },
    getKey : function( key ){
        return key in this.map[this.way];
    }
};
Storage.prototype.constructor = Storage;

這樣看起來舒服多了,全部共享出去了,所有的實(shí)例都可以使用了,哈哈哈,親測(cè)可用,如遇到問題可以隨時(shí)交流,發(fā)現(xiàn)bug,也要及時(shí)反饋哈

關(guān)于prototype的幾點(diǎn)說明

只要函數(shù)被聲明了,就會(huì)產(chǎn)生對(duì)應(yīng)的原型對(duì)象,即所有的函數(shù)都有原型對(duì)象,可以通過functionName.prototype訪問原型對(duì)象。既然functionName.prototype是對(duì)象那么就可以通過"."的方式設(shè)置屬性和方法如:

function People(){};
People.prototype.name = "阿里巴巴";
People.prototype.get = function(){
    console.log(this.name);
};
var people = new People(); // 實(shí)例化
console.log( people.name ); // 阿里巴巴
console.log( people.get() );// 阿里巴巴
console.log( people.constructor ); // People函數(shù)
console.log( people.__proto__.constructor ); // People函數(shù)

自定義方法優(yōu)化
function Memory( key, way ){
    this.way = way;
    this.key = key;
};
Memory.prototype = {
    constructor : Memory,
    map : {
        "session" : window.sessionStorage,
        "local" : window.localStorage
    },
    setItem : function( value ){
        this.map[this.way].setItem( this.key, JSON.stringify( value ) )
    },
    getItem : function(){
        return JSON.parse( this.map[this.way].getItem( this.key ) );
    },
    removeItem : function(){
        this.map[this.way].removeItem( this.key );
    },
    clear : function(){
        this.map[this.way].clear();
    },
    getKey : function(){
        return this.key in this.map[this.way];
    }
};
var myStore = new Memory("key","session"); // 實(shí)例化一個(gè)對(duì)象,指定鍵值和存儲(chǔ)方式
myStore.setItem("value"); // 指定存儲(chǔ)的數(shù)據(jù),可以是字符串、數(shù)字、json對(duì)象、數(shù)組。無需轉(zhuǎn)換數(shù)據(jù)類型
myStore.getItem(); // 獲取存儲(chǔ)的數(shù)據(jù),無需指定鍵值。

myStore.removeItem(); // 移除數(shù)據(jù)

var session = new Memory("clear","session");
session.clear(); // 清空所有的 session 存儲(chǔ)

var local = new Memory("clear","local");
local.clear(); // 清空所有的 local 存儲(chǔ)





文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/82711.html

相關(guān)文章

  • sessionStoragelocalStorage

    摘要:前端存儲(chǔ)和簡(jiǎn)述曾在項(xiàng)目中多次使用過和來存放,但一直未深入了解。以為例他們均只能存儲(chǔ)字符串類型的對(duì)象存儲(chǔ)大小都為都遵守同源策略不同點(diǎn)有期限,當(dāng)窗口或?yàn)g覽器關(guān)閉時(shí)就會(huì)被銷毀。本文如有錯(cuò)誤,歡迎指出。 H5前端存儲(chǔ) localStorage 和 sessionStorage 簡(jiǎn)述 曾在項(xiàng)目中多次使用過localStorage和sessionStorage來存放token,但一直未深入了解。近...

    wujl596 評(píng)論0 收藏0
  • 印象最深的一個(gè)bug:sessionStorage緩存在移動(dòng)端失效

    摘要:移動(dòng)端緩存失效是我印象最深的一個(gè)之一,為啥呢,因?yàn)檫@個(gè)問題導(dǎo)致我加班到很晚。的生命周期是僅在當(dāng)前會(huì)話下有效。引入了一個(gè)瀏覽器窗口的概念,是在同源的窗口中始終存在的數(shù)據(jù)。無bug,不程序:作為程序員的我,不是修bug就是在寫bug的路上。  移動(dòng)端sessionStorage緩存失效是我印象最深的一個(gè)bug之一,為啥呢,因?yàn)檫@個(gè)問題導(dǎo)致我加班到很晚。在現(xiàn)在看來就是一個(gè)簡(jiǎn)單的概念問題。在我剛工作...

    yuxue 評(píng)論0 收藏0
  • sessionStorgelocalStorage的使用-踩坑記_09

    摘要:的使用屬性允許你訪問一個(gè)對(duì)象。它與相似,不同之處在于里面存儲(chǔ)的數(shù)據(jù)沒有過期時(shí)間設(shè)置,而存儲(chǔ)在里面的數(shù)據(jù)在頁面會(huì)話結(jié)束時(shí)會(huì)被清除。頁面會(huì)話在瀏覽器打開期間一直保持,并且重新加載或恢復(fù)頁面仍會(huì)保持原來的頁面會(huì)話。 sessionStorge的使用 sessionStorage 屬性允許你訪問一個(gè) session Storage 對(duì)象。它與 localStorage 相似,不同之處在于 lo...

    Jochen 評(píng)論0 收藏0
  • sessionStorgelocalStorage的使用-踩坑記_09

    摘要:的使用屬性允許你訪問一個(gè)對(duì)象。它與相似,不同之處在于里面存儲(chǔ)的數(shù)據(jù)沒有過期時(shí)間設(shè)置,而存儲(chǔ)在里面的數(shù)據(jù)在頁面會(huì)話結(jié)束時(shí)會(huì)被清除。頁面會(huì)話在瀏覽器打開期間一直保持,并且重新加載或恢復(fù)頁面仍會(huì)保持原來的頁面會(huì)話。 sessionStorge的使用 sessionStorage 屬性允許你訪問一個(gè) session Storage 對(duì)象。它與 localStorage 相似,不同之處在于 lo...

    PrototypeZ 評(píng)論0 收藏0
  • 客戶端數(shù)據(jù)存儲(chǔ) --- web storage From 《高程3》

    摘要:的目的就是取代進(jìn)行大量的本地?cái)?shù)據(jù)存儲(chǔ),其中不能進(jìn)行跨會(huì)話存儲(chǔ),這可以使用彌補(bǔ)。刪除由指定的名值對(duì)兒。使用方法存儲(chǔ)數(shù)據(jù)使用屬性存儲(chǔ)數(shù)據(jù)使用方法讀取數(shù)據(jù)使用屬性讀取數(shù)據(jù)一般來說,對(duì)存儲(chǔ)空間大小的限制都是以每個(gè)源協(xié)議域和端口為單位的。 前言 本文首先介紹web storage和Cookie的對(duì)比,解釋web storage的優(yōu)勢(shì);隨后指出怎樣使用插firebug插件的擴(kuò)展firestorag...

    wangbinke 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

RancherLabs

|高級(jí)講師

TA的文章

閱讀更多
最新活動(dòng)
閱讀需要支付1元查看
<