js singleton mode to create pop-up window instance sharing
1. Understand the singleton mode
Definition of singleton mode: Ensure that a class has only one instance and provide a global access point to access it
Singleton mode The core: to ensure that there is only one instance and provide global access
2. Singleton mode in JavaScript
In js, we often use global variables as singleton mode. For example:
var a={};
Why a can be used as a global variable, because it meets the following two conditions:
1. Object a is unique
2. a is defined in the global scope, providing global access
Note: However, it is recommended to use namespaces in js to reduce the number of global variables
3. Lazy singleton
Lazy singleton: an object instance created only when needed
Purpose: There are two buttons on the page. When clicked, a response pop-up window needs to be displayed and Load the corresponding css file
Note: Some developers are used to writing in the page when it loads, and then setting the hidden state, which will waste DOM nodes
The following is the implementation code:
1. Main page
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>单例模式</title> <style type="text/css"> body{ background: #fffff2; } h3{ text-align: center; } </style> </head> <body> <h3>创建唯一的窗口</h3> <button type="button" id="btn1">创建p1</button> <button type="button" id="btn2">创建p2</button> </body> <script type="text/javascript"> /** * 管理单例 */ var getSingle=function(fn){ var result; return function(){ return result || (result=fn.apply(this,arguments)); } }; // 弹框关闭打开 function LayerAction(){ this.layer=null;//弹窗element if(typeof this.setLayer !== "function"){ // 设置弹窗 LayerAction.prototype.setLayer=function(layer){ if(!layer){ console.error("参数不完整,必须传入layer"); return; }else{ this.layer=layer; } }; // 显示弹窗 LayerAction.prototype.showLayer=function(){ this.layer.style.display="block"; }; // 关闭弹窗 LayerAction.prototype.closeLayer= function(){ this.layer.style.display="none"; } ; } } /** * p1弹窗 */ var p1={ p1Layer:null, layerAction: new LayerAction(), // 创建p1弹窗 createp1Layer:function(){ var p=document.createElement("p"); p.innerHTML="我是p1"; p.style.display='none'; p.id="p1"; document.body.appendChild(p); var closeBtn=document.createElement("span"); closeBtn.innerText="关闭"; closeBtn.id="closep1"; p.appendChild(closeBtn); return p; }, // 引入p1弹窗样式列表 createp1Style: function() { var styleElement = document.createElement('link'); styleElement.type = 'text/css'; styleElement.href = 'p1.css'; styleElement.rel = 'stylesheet'; document.getElementsByTagName('head')[0].appendChild(styleElement); console.log(document.getElementsByTagName('head')[0].innerHTML); return styleElement }, // 为关闭按钮绑定事件 bindActionForCloseLayer: function(){ var that=p1; document.getElementById("closep1").onclick=function(){ that.layerAction.closeLayer(); } }, // 调用弹窗1 startp1Layer: function(){ var createp1singleLayer=getSingle(this.createp1Layer); var createp1singleStyle=getSingle(this.createp1Style); var bindCloseEvent=getSingle(this.bindActionForCloseLayer); var that=this; document.getElementById("btn1").onclick=function(){ createp1singleStyle(); that.p1Layer=createp1singleLayer(); that.layerAction.setLayer(that.p1Layer); that.layerAction.showLayer(); bindCloseEvent(); } } }; p1.startp1Layer(); /** * p2弹窗 */ var p2={ p2Layer:null, layerAction: new LayerAction(), // 创建p2弹窗 createp2Layer: function(){ var p=document.createElement("p"); p.innerHTML="我是p2"; p.style.display='none'; p.id="p2"; document.body.appendChild(p); var closeBtn=document.createElement("span"); closeBtn.innerText="关闭"; closeBtn.id="closep2"; p.appendChild(closeBtn); return p; }, // 引入p2弹窗样式列表 createp2Style: function () { var styleElement = document.createElement('link'); styleElement.type = 'text/css'; styleElement.href = 'p2.css'; styleElement.rel = 'stylesheet'; document.getElementsByTagName('head')[0].appendChild(styleElement); console.log(document.getElementsByTagName('head')[0].innerHTML); return styleElement }, // 为关闭按钮绑定事件 bindActionForCloseLayer: function(){ var that=p2; document.getElementById("closep2").onclick=function(){ that.layerAction.closeLayer(); } }, // 调用弹窗2 startp2Layer: function(){ var createp2singleLayer=getSingle(this.createp2Layer); var createp2singleStyle=getSingle(this.createp2Style); var bindCloseEvent=getSingle(this.bindActionForCloseLayer); var that=this; document.getElementById("btn2").onclick=function(){ createp2singleStyle(); that.p2Layer=createp2singleLayer(); that.layerAction.setLayer(that.p2Layer); that.layerAction.showLayer(); bindCloseEvent(); } } } p2.startp2Layer(); </script> </html>
2. p1.css
/** * Description: * Created by wxy on 2018/2/13 11:02 */ #p2{ width: 500px; height: 300px; background: #ffdd00; color: #fff; position: fixed; top: 50%; left: 50%; transform: translate(-50%,-50%); } #closep2{ cursor: pointer; margin-right: 5px; margin-top: 5px; float: right; border: 1px solid #fff; }
3. p2.css
/** * Description: style of p1 * Created by wxy on 2018/2/13 11:01 */ #p1{ width: 500px; height: 300px; background: #0b0a0a; color: #fff; position: fixed; top: 50%; left: 50%; transform: translate(-50%,-50%); } #closep1{ cursor: pointer; margin-right: 5px; margin-top: 5px; float: right; border: 1px solid #fff; }
Related recommendations:
Two options for js singleton mode_javascript skills
Summary of NodeJS singleton mode, adapter mode, decoration mode, and observer mode
js Singleton Pattern Detailed Example_Basic Knowledge
The above is the detailed content of js singleton mode to create pop-up window instance sharing. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Quark Netdisk and Baidu Netdisk are very convenient storage tools. Many users are asking whether these two softwares are interoperable? How to share Quark Netdisk to Baidu Netdisk? Let this site introduce to users in detail how to save Quark network disk files to Baidu network disk. How to save files from Quark Network Disk to Baidu Network Disk Method 1. If you want to know how to transfer files from Quark Network Disk to Baidu Network Disk, first download the files that need to be saved on Quark Network Disk, and then open the Baidu Network Disk client. , select the folder where the compressed file is to be saved, and double-click to open the folder. 2. After opening the folder, click "Upload" in the upper left corner of the window. 3. Find the compressed file that needs to be uploaded on your computer and click to select it.

1. First, we enter NetEase Cloud Music, and then click on the software homepage interface to enter the song playback interface. 2. Then in the song playback interface, find the sharing function button in the upper right corner, as shown in the red box in the figure below, click to select the sharing channel; in the sharing channel, click the "Share to" option at the bottom, and then select the first "WeChat Moments" allows you to share content to WeChat Moments.

Recently, Baidu Netdisk Android client has ushered in a new version 8.0.0. This version not only brings many changes, but also adds many practical functions. Among them, the most eye-catching is the enhancement of the folder sharing function. Now, users can easily invite friends to join and share important files in work and life, achieving more convenient collaboration and sharing. So how do you share the files you need to share with your friends? Below, the editor of this site will give you a detailed introduction. I hope it can help you! 1) Open Baidu Cloud APP, first click to select the relevant folder on the homepage, and then click the [...] icon in the upper right corner of the interface; (as shown below) 2) Then click [+] in the "Shared Members" column 】, and finally check all

Mango TV has various types of movies, TV series, variety shows and other resources, and users can freely choose to watch them. Mango TV members can not only watch all VIP dramas, but also set the highest definition picture quality to help users watch dramas happily. Below, the editor will bring you some free Mango TV membership accounts for users to use, hurry up and take a look Take a look. Mango TV latest member account free sharing 2023: Note: These are the latest member accounts collected, you can log in directly and use them, do not change the password at will. Account number: 13842025699 Password: qds373 Account number: 15804882888 Password: evr6982 Account number: 13330925667 Password: jgqae Account number: 1703

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Title: To solve the problem that Discuz WeChat shares cannot be displayed, specific code examples are needed. With the development of the mobile Internet, WeChat has become an indispensable part of people's daily lives. In website development, in order to improve user experience and expand website exposure, many websites will integrate WeChat sharing functions, allowing users to easily share website content to Moments or WeChat groups. However, sometimes when using open source forum systems such as Discuz, you will encounter the problem that WeChat shares cannot be displayed, which brings certain difficulties to the user experience.

HP printers are essential printing equipment in many offices. Installing the printer driver on the computer can perfectly solve problems such as the printer being unable to connect. So how to install HP printer driver? The editor below will introduce you to two HP printer driver installation methods. The first method: download the driver from the official website 1. Search the HP China official website in the search engine, and in the support column, select [Software and Drivers]. 2. Select the [Printer] category, enter your printer model in the search box, and click [Submit] to find your printer driver. 3. Select the corresponding printer according to your computer system. For win10, select the driver for win10 system. 4. After downloading successfully, find it in the folder

Tomato Novels is a rich treasure house of novels, which gathers a large number of high-quality novel resources. Here, you can choose your favorite novels from many different types of novels according to your preferences. For those of you who love reading, this is undoubtedly a literary world where you can fly freely. Sometimes when you encounter your favorite reading material, it’s like sharing it with friends to read together, but many users don’t know exactly how to share it, so this tutorial guide will bring you a detailed introduction to the guide, for players who want to know more Come and read along with this article! How to share Tomato novels with friends? 1. Open Tomato Novel, click to enter the novel, and click the share icon in the upper right corner. 2. Select the sharing channel. Here I take sharing to WeChat friends as an example. 3. Click Share. 4. You can check
