JS存储

Storage

localStorage

永久性的存储方法

  • 属性
    • length
  • 方法
    • Storage.key(index): 返回存储中的第n个key名称
    • Storage.getItem(key)
    • Storage.setItem(key,value)
    • Storage.removeItem(key)
    • Storage.clear()

sessionStorage

临时存储方法,会话关闭,存储内容会被清除

  • getItem(key)
  • setItem(key,value)

Storage封装

class Cache {
    constructor(isLocal = true){
        this.storage = isLocal?localStorage:sessionStorage
    }
    setCache(key,value){
        if (!value) {
            throw new Error('value error: value 必须有值')
        }
        this.storage.setItem(key,JSON.stringify(value))
    }
    getCache(key){
        const result = this.storage.getItem(key)
        if(result){
            return JSON.parse(result)
        }
    }
    removeCache(key){
        this.storage.removeItem(key,value)
    }
    clear(key,value){
        this.storage.clear()
    }
}
const localCache = new Cache()
const sessionCache = new Cache(false)

cookie

posted @   转角90  阅读(21)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
点击右上角即可分享
微信分享提示