搜尋
首頁web前端js教程js單例模式之建立彈跳窗實例分享
js單例模式之建立彈跳窗實例分享Mar 07, 2018 am 11:20 AM
javascript分享實例

一、了解單例模式

    單例模式的定義:保證一個類別只有一個實例,並提供一個存取他的全域存取點

##    單例模式的核心:是確保只有一個實例,並提供全域存取

二、javascript中的單例模式

    在js中,我們常常會把全域變數當作單例模式來使用,例如:

        var a={};

    為什麼a可以當做全域變數來使用呢,因為其符合下列兩個條件:

 #   

##        2、a定義在全域作用域下,提供了全域存取

    注意:但在js中建議使用命名空間,以減少全域變數的數量

三、

三、惰性單例

    惰性單例:在需要的時候才建立的物件實例

    用途:在頁面中有兩個按鈕,點選的時候需要顯示回應彈窗並載入對應的css檔案

    注意:有些開發人員習慣在頁面載入時就寫進去,然後設定隱藏狀態,這樣就會浪費DOM節點

下面是實作程式碼:

1、主頁

<!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=&#39;none&#39;;
            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(&#39;link&#39;);
            styleElement.type = &#39;text/css&#39;;
            styleElement.href = &#39;p1.css&#39;;
            styleElement.rel = &#39;stylesheet&#39;;
            document.getElementsByTagName(&#39;head&#39;)[0].appendChild(styleElement);
            console.log(document.getElementsByTagName(&#39;head&#39;)[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=&#39;none&#39;;
            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(&#39;link&#39;);
            styleElement.type = &#39;text/css&#39;;
            styleElement.href = &#39;p2.css&#39;;
            styleElement.rel = &#39;stylesheet&#39;;
            document.getElementsByTagName(&#39;head&#39;)[0].appendChild(styleElement);
            console.log(document.getElementsByTagName(&#39;head&#39;)[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;
}

相關推薦:

js單例模式的兩個方案_javascript技巧

NodeJS單例模式,適配器模式,裝飾模式,觀察者模式總結

js單例模式詳解實例_基礎知識

以上是js單例模式之建立彈跳窗實例分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
es6数组怎么去掉重复并且重新排序es6数组怎么去掉重复并且重新排序May 05, 2022 pm 07:08 PM

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

JavaScript的Symbol类型、隐藏属性及全局注册表详解JavaScript的Symbol类型、隐藏属性及全局注册表详解Jun 02, 2022 am 11:50 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

原来利用纯CSS也能实现文字轮播与图片轮播!原来利用纯CSS也能实现文字轮播与图片轮播!Jun 10, 2022 pm 01:00 PM

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

JavaScript对象的构造函数和new操作符(实例详解)JavaScript对象的构造函数和new操作符(实例详解)May 10, 2022 pm 06:16 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

javascript怎么移除元素点击事件javascript怎么移除元素点击事件Apr 11, 2022 pm 04:51 PM

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

JavaScript面向对象详细解析之属性描述符JavaScript面向对象详细解析之属性描述符May 27, 2022 pm 05:29 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

foreach是es6里的吗foreach是es6里的吗May 05, 2022 pm 05:59 PM

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。

整理总结JavaScript常见的BOM操作整理总结JavaScript常见的BOM操作Jun 01, 2022 am 11:43 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能