• Storage 储存操作


    class StorageFn {
        constructor () {
            this.ls = window.localStorage;
            this.ss = window.sessionStorage;
        }
    
        /*-----------------cookie---------------------*/
        /*设置cookie*/
        setCookie (name, value, day) {
            var setting = arguments[0];
            if (Object.prototype.toString.call(setting).slice(8, -1) === 'Object'){
                for (var i in setting) {
                    var oDate = new Date();
                    oDate.setDate(oDate.getDate() + day);
                    document.cookie = i + '=' + setting[i] + ';expires=' + oDate;
                }
            }else{
                var oDate = new Date();
                oDate.setDate(oDate.getDate() + day);
                document.cookie = name + '=' + value + ';expires=' + oDate;
            }
    
        }
    
        /*获取cookie*/
        getCookie (name) {
            var arr = document.cookie.split('; ');
            for (var i = 0; i < arr.length; i++) {
                var arr2 = arr[i].split('=');
                if (arr2[0] == name) {
                    return arr2[1];
                }
            }
            return '';
        }
    
        /*删除cookie*/
        removeCookie (name) {
            this.setCookie(name, 1, -1);
        }
    
        /*-----------------localStorage---------------------*/
        /*设置localStorage*/
        setLocal(key, val) {
            var setting = arguments[0];
            if (Object.prototype.toString.call(setting).slice(8, -1) === 'Object'){
                for(var i in setting){
                    this.ls.setItem(i, JSON.stringify(setting[i]))
                }
            }else{
                this.ls.setItem(key, JSON.stringify(val))
            }
    
        }
    
        /*获取localStorage*/
        getLocal(key) {
            if (key) return JSON.parse(this.ls.getItem(key))
            return null;
    
        }
    
        /*移除localStorage*/
        removeLocal(key) {
            this.ls.removeItem(key)
        }
    
        /*移除所有localStorage*/
        clearLocal() {
            this.ls.clear()
        }
    
        /*-----------------sessionStorage---------------------*/
        /*设置sessionStorage*/
        setSession(key, val) {
            var setting = arguments[0];
            if (Object.prototype.toString.call(setting).slice(8, -1) === 'Object'){
                for(var i in setting){
                    this.ss.setItem(i, JSON.stringify(setting[i]))
                }
            }else{
                this.ss.setItem(key, JSON.stringify(val))
            }
    
        }
    
        /*获取sessionStorage*/
        getSession(key) {
            if (key) return JSON.parse(this.ss.getItem(key))
            return null;
    
        }
    
        /*移除sessionStorage*/
        removeSession(key) {
            this.ss.removeItem(key)
        }
    
        /*移除所有sessionStorage*/
        clearSession() {
            this.ss.clear()
        }
    
    }
    

      

  • 相关阅读:
    关于JS事件冒泡与JS事件代理(事件委托)
    异步、同步和阻塞、非阻塞
    大数据高并发
    前段clam安装
    JavaScript动态修改CSS
    键盘按键js效果
    键盘键值表总结
    移动端不可缩放
    JS基本语法
    计算几何——ICPC Latin American Regional Contests 2019 B
  • 原文地址:https://www.cnblogs.com/caoruichun/p/10520563.html
Copyright © 2020-2023  润新知