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 id="创建唯一的窗口">创建唯一的窗口</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!

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools

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

Atom editor mac version download
The most popular open source editor
