单例模式工程运用

单例模式是什么

一个类,只允许,有一个实例,并且能通过全局变量访问到

案例

js 复制代码
// 实现Storage,使得该对象为单例,基于 localStorage 进行封装。实现方法 setItem(key,value) 和 getItem(key)。
class Storage {
  static getInstance() {
    if (!Storage.instance) {
      Storage.instance = new Storage();
    }
    return Storage.instance;
  }

  getItem(key, value) {
    return window.localStorage.getItem(key);
  }
  setItem(key, value) {
    return window.localStorage.setItem(key, value);
  }
}
let storage1 = Storage.getInstance();
let storage2 =  Storage.getInstance();
storage1 === storage2 //true

如果还要求写一个全局唯一弹出窗呢

  1. 在父盒子中上下左右居中
  2. 宽高不固定,由内容撑开
  3. 外层有灰色蒙版,透明度为 0.5

关键点

  1. 借助flex 居中
  2. 内容如何自适应撑开
  3. 蒙层注意层级设置
js 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 1. 在父盒子中上下左右居中
2. 宽高不固定,由内容撑开
3. 外层有灰色蒙版,透明度为 0.5
4. 自下向上弹出(可选) -->
    <!-- 弹出窗组件是要求手动创建的,不是预先写死一些dom结构,在组件内 -->
    <!-- <div class="modal"></div> -->
    <!-- 原生开发都忘记了函数必须加上括号了 -->
    <div onclick="modalOpen()">打开</div>
    <div onclick="modalClose()">关闭</div>
    <!-- 所谓的蒙层就是遮罩层    z-index: 999; 忘记了-->
    <style>
      #modalWrapper {
        background-color: gray;
        opacity: 0.5;
        width: 100%;
        height: 100%;
        z-index: 999;
        position: fixed;
        left: 0;
        top: 0;
        display: none;
        justify-content: center;
        align-items: center;
      }
      #modal {
        width: fit-content;
        height: fit-content;
        background-color: aqua;
      }
    </style>
    <script>
      // 创建弹窗容器

      const getModal = (() => {
        let modalInstance = null;
        return () => {
          if (modalInstance) return modalInstance;
          const modal = document.createElement("div");
          const modalWrapper = document.createElement("div");
          modalWrapper.appendChild(modal);
          modalWrapper.id = "modalWrapper";
          modal.id = "modal";
          modal.innerHTML = "我是弹出创";
          modalInstance = modalWrapper;
          // 弹出创组件,为何执着于,一定要跟在body内
          document.body.appendChild(modalWrapper);
          return modalInstance;
        };
      })();
      const modalOpen = () => {
        let modal = getModal();
        modal.style.display = "flex";
      };

      const modalClose = () => {
        let modal = getModal();
        modal.style.display = "none";
      };
    </script>
  </body>
</html>
相关推荐
用户033307413952 小时前
TypeScript using 与 AsyncDisposableStack:在真实服务端代码中协调多资源清理
前端
daisychey2 小时前
用 Playwright 做多平台内容分发,我踩过的 8 个坑
前端
IT_陈寒3 小时前
为什么你应该学习JavaScript?
前端·人工智能·后端
大龄秃头程序员3 小时前
iOS冷启动监控Demo
前端
光影少年3 小时前
为什么 JavaScript 中 0.1 + 0.2 !== 0.3,如何让其相等?
前端·javascript·算法
掘金酱4 小时前
[稀土掘金 × 火山引擎] AI用量周榜冲刺赛|获奖名单公示
前端·人工智能·后端
泡泡oO4 小时前
“如果你还在用Superpowers,那我不要和你说话”
前端·后端·全栈
维克兜率天4 小时前
趋势过滤:避免被“假突破“反复打脸
前端
8年区块链老兵5 小时前
一篇文章教会你:用区块高度读懂比特币链上的每一笔交易
前端
xixichensh5 小时前
前端转做AI程序员第一步——开发一个AI流式对话Demo
前端