单例模式工程运用

单例模式是什么

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

案例

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>
相关推荐
林希_Rachel_傻希希4 分钟前
手写Promise最终版本
前端·javascript·面试
visnix6 分钟前
AI大模型-LLM原理剖析到训练微调实战(第二部分:大模型核心原理与Transformer架构)
前端·llm
老妪力虽衰7 分钟前
零基础的小白也能通过AI搭建自己的网页应用
前端
褪色的笔记簿10 分钟前
在 Vue 项目里管理弹窗组件:用 ref 还是用 props?
前端·vue.js
Danny_FD12 分钟前
使用Taro实现微信小程序仪表盘:使用canvas实现仪表盘(有仪表盘背景,也可以用于Web等)
前端·taro·canvas
掘金安东尼21 分钟前
VSCode V1.107 发布(2025 年 11 月)
前端·visual studio code
一只小阿乐24 分钟前
前端vue3 web端中实现拖拽功能实现列表排序
前端·vue.js·elementui·vue3·前端拖拽
AAA阿giao29 分钟前
从“操纵绳子“到“指挥木偶“:Vue3 Composition API 如何彻底改变前端开发范式
开发语言·前端·javascript·vue.js·前端框架·vue3·compositionapi
TextIn智能文档云平台35 分钟前
图片转文字后怎么输入大模型处理
前端·人工智能·python
专注前端30年37 分钟前
在日常开发项目中Vue与React应该如何选择?
前端·vue.js·react.js