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

  • 相关阅读:
    Ubuntu中的vsftpd配置
    Ubuntu学习-增加更新源及安装软件及卸载软件
    Ubuntu16.04下安装sublime text3
    需要读的书
    同一机器 部署 两个 jboss
    log4j 总结 精华
    oracle 笔记
    oracle 用户 多个表空间
    json
    json 基础
  • 原文地址:https://www.cnblogs.com/mafeifan/p/5580908.html
Copyright © 2011-2022 走看看