zoukankan      html  css  js  c++  java
  • ES6 localStorage 类库

    无意中看到的,记录下。

    用到了es6语法。支持在js中写构造函数

    class CovLocalDB {
        constructor (name) {
            this.LS = null
            this.name = name
            this.checkLS()
            this.init(name)
        }
    
        checkLS () {
            if (window && window.localStorage) {
                this.LS = window.localStorage
                // console.log('localStorage is there?')
            } else {
                console.log('localStorage is there?')
            }
        }
    
        init (name) {
            if (this.LS) {
                if (this.LS[name]) {
                    this.data = JSON.parse(this.LS[name])
                } else {
                    this.data = {}
                }
            }
        }
    
        set (uri, data) {
            this.data[uri] = data
            if (this.LS) {
                this.LS[this.name] = JSON.stringify(this.data)
            }
        }
    
        get (uri) {
            if (this.data[uri]) {
                return this.data[uri]
            }
            return false
        }
    }
    
    
    const DB = new CovLocalDB('my-db')
    DB.set('name', 'finley');
    console.log(DB.get('name'));

    关于class

    class Point {
      constructor(){
        // ...
      }
    
      toString(){
        // ...
      }
    
      toValue(){
        // ...
      }
    }
    
    // 等同于
    
    Point.prototype = {
      toString(){},
      toValue(){}
    };

    参考:http://es6.ruanyifeng.com/#docs/class

  • 相关阅读:
    apscheduler 踩坑
    fastapi 导出excel文件
    python flask 使用日志
    git 头指针游离问题
    C# 连接mysql填坑
    前端项目proxy小问题
    需完善--日志框架
    依赖<dependency>的scope了解
    git 退回到指定tag版本
    git切换远程仓库地址
  • 原文地址:https://www.cnblogs.com/mafeifan/p/5580908.html
Copyright © 2011-2022 走看看