美文网首页
模拟实现一个 localStorage

模拟实现一个 localStorage

作者: TerdShow | 来源:发表于2019-07-11 16:22 被阅读0次
class localStorageMock {
    constructor() {
      this.store = {};
    };
    getItem(key) {
      return this.store[key] || null;
    };
    setItem(key, value) {
      this.store[key] = value.toString();
    };
    removeItem(key) {
      delete this.store[key];
    };
    clear() {
      this.store = {};
    };
  };
  window.localStorage2 = new localStorageMock();

  localStorage2.setItem('a', 1);
  localStorage2.getItem('a');
  console.log(localStorage2.getItem('a'));

相关文章

网友评论

      本文标题:模拟实现一个 localStorage

      本文链接:https://www.haomeiwen.com/subject/rtedkctx.html