J-DuYa / DY-Book

迁移知识点

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

单例模式

J-DuYa opened this issue · comments

  • 概念讲解

  • 原理解析

  • 代码实现

# class 实现
class Storage {
  static getInstance () {
     if (!Storage.instance) {
       Storage.instance = new Storage();
    }
    return Storage.instance;
  }

  getItem (key) {
    return localstorage.getItem(key);
  }

  setItem (key, value) {
    localstorage.setItem(key, value);
  }

}