定义

是能被实例化一次的类或者对象(只能存在一个)

特点

  • 只允许实例化一次的对象类
  • 对于十分复杂的对象类,往往可以节省资源占用
  • 通常也被用来管理命名空间

作用

管理命名空间,管理数据,方法存储

应用

  • 一些代码库中命名空间就是通过单例面试实现的
  • 管理数据的存储,例如模拟静态变量

    coding

    ```html <!DOCTYPE html>

  1. <a name="mNy9H"></a>
  2. # 举例-单例模式_自定义弹出层
  3. ```html
  4. <!DOCTYPE html>
  5. <html lang="en">
  6. <head>
  7. <meta charset="UTF-8" />
  8. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  9. <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  10. <title>单例模式_自定义弹出层</title>
  11. <style>
  12. div.tip {
  13. width: 500px;
  14. height: fit-content;
  15. position: fixed;
  16. top: 0;
  17. left: 0;
  18. right: 0;
  19. bottom: 0;
  20. margin: auto;
  21. background-color: #fff;
  22. background-clip: padding-box;
  23. border-radius: 4px;
  24. box-shadow: 0 4px 12px rgb(0 0 0 / 15%);
  25. pointer-events: auto;
  26. display: none;
  27. }
  28. div.tip > .top {
  29. background-color: skyblue;
  30. padding: 16px 24px;
  31. color: rgba(0, 0, 0, 0.65);
  32. border-bottom: 1px solid #e8e8e8;
  33. border-radius: 4px 4px 0 0;
  34. line-height: 22px;
  35. }
  36. div.tip > .top > span {
  37. float: right;
  38. width: 20px;
  39. height: 20px;
  40. border-radius: 50%;
  41. cursor: pointer;
  42. background-color: #fff;
  43. font-size: 16px;
  44. text-align: center;
  45. line-height: 20px;
  46. }
  47. .tip > .content {
  48. padding: 24px;
  49. font-size: 14px;
  50. line-height: 1.5;
  51. word-wrap: break-word;
  52. }
  53. .tip > .btns {
  54. display: flex;
  55. justify-content: flex-end;
  56. padding: 10px 16px;
  57. background: transparent;
  58. border-top: 1px solid #e8e8e8;
  59. border-radius: 0 0 4px 4px;
  60. }
  61. </style>
  62. </head>
  63. <body>
  64. <div class="tip">
  65. <div class="top">
  66. 标题
  67. <span>X</span>
  68. </div>
  69. <div class="content">
  70. <p>提示内容</p>
  71. </div>
  72. <div class="btns">
  73. <button>取消</button>
  74. <button>确定</button>
  75. </div>
  76. </div>
  77. <script>
  78. const Tip = (() => {
  79. class Tip {
  80. constructor() {
  81. this.ele = document.createElement("div");
  82. this.ele.className = "tip";
  83. document.body.appendChild(this.ele);
  84. this.callback = () => {};
  85. this.bindEvent();
  86. }
  87. setContent(txt) {
  88. this.ele.innerHTML = `
  89. <div class="top">标题
  90. <span class="close">X</span>
  91. </div>
  92. <div class="content">
  93. <p>${txt}</p>
  94. </div>
  95. <div class="btns">
  96. <button class="cancel">取消</button>
  97. <button class="ok">确定</button>
  98. </div>
  99. `;
  100. this.ele.style.display = "block";
  101. }
  102. bindEvent() {
  103. this.ele.addEventListener("click", (e = window.event) => {
  104. const target = e.target || e.srcElement;
  105. const { className } = target;
  106. switch (className) {
  107. case "close":
  108. this.ele.style.display = "none";
  109. break;
  110. case "cancel":
  111. this.ele.style.display = "none";
  112. this.callback(false);
  113. break;
  114. case "ok":
  115. this.ele.style.display = "none";
  116. this.callback(true);
  117. break;
  118. }
  119. });
  120. }
  121. setStyle(val) {
  122. this.ele.querySelector(".top").style.backgroundColor = val;
  123. }
  124. }
  125. let instance = null;
  126. return function singleTon(options = {}, cb = function () {}) {
  127. const { txt = "Hello World", topBG = "skyblue" } = options;
  128. if (!instance) instance = new Tip();
  129. instance.setContent(txt);
  130. instance.setStyle(topBG);
  131. instance.callback = cb;
  132. return instance;
  133. };
  134. })();
  135. Tip(
  136. {
  137. txt: "Hello World",
  138. topBG: "#1890ff",
  139. },
  140. (res) => {
  141. console.log("print: ", res);
  142. }
  143. );
  144. </script>
  145. </body>
  146. </html>