zoukankan      html  css  js  c++  java
  • localStorage 缓存方法增强[code snippet]

    /* 
    Enhance localStorage -- Start --
    usage: cusLocalStorage.setItem('infos','name','jayce) ==> "infos":"{'name':'jayce'}"
    cusLocalStorage.getItem('infos','name') ==> "jayce"
    cusLocalStorage.removeItem('infos','name') ==> "'infos':'{}'"
    */
    
    !((localStorage) => {
      window.cusLocalStorage = {
        setItem: function (objkey, fieldkey, fieldval) {
          this.errorCatch(objkey, fieldkey, fieldval)
          let getObj = localStorage.getItem(objkey)
          if (getObj == null) {
            localStorage.setItem(
              objkey,
              JSON.stringify({
                [fieldkey]: fieldval,
              })
            )
          } else {
            let JsonObj = JSON.parse(getObj)
            JsonObj[fieldkey] = fieldval
            localStorage.setItem(objkey, JSON.stringify(JsonObj))
          }
        },
        getItem: function (objkey, fieldkey) {
          this.errorCatch(objkey, fieldkey)
          let getObj = localStorage.getItem(objkey)
          if (getObj != null) {
            return JSON.parse(getObj)[fieldkey]
          } else {
            return null
          }
        },
        removeItem: function (objkey, fieldkey) {
          this.errorCatch(objkey, fieldkey)
          let getObj = localStorage.getItem(objkey)
          if (getObj != null) {
            try {
              let jsonObj = JSON.parse(getObj)
              delete jsonObj[fieldkey]
              localStorage.setItem(objkey, JSON.stringify(jsonObj))
            } catch (e) {}
          }
        },
        errorCatch: function () {
          // prettier-ignore
          if (typeof arguments[0] != 'string' || typeof arguments[1] != 'string') {
        throw new Error('param1 AND param2, must be a string expected !')
      }
        },
      }
    })(localStorage)
    
    /* Enhance localStorage -- End -- */
    
  • 相关阅读:
    异常处理(五)
    抽象与接口(七)
    MYSQL创建数据库时候直接指定编码和排序规则
    C#基本语法(一)
    C#中字符串处理(三)
    Ajax实例化(五)
    C#函数(二)
    MemberCache学习摘要
    ORACLE 根据日期范围自动日期table
    动态执行SQL语句,并输出参数
  • 原文地址:https://www.cnblogs.com/jaycethanks/p/15722119.html
Copyright © 2011-2022 走看看