ホームページ  >  記事  >  ウェブフロントエンド  >  js シングルトン モードでポップアップ ウィンドウ インスタンス共有を作成する

js シングルトン モードでポップアップ ウィンドウ インスタンス共有を作成する

小云云
小云云オリジナル
2018-03-07 11:20:101714ブラウズ

1. シングルトン パターンを理解する

シングルトン パターンの定義: クラスにインスタンスが 1 つだけあることを確認し、それにアクセスするためのグローバル アクセス ポイントを提供します

シングルトン パターンの核心: インスタンスが 1 つだけ存在することを確認し、グローバル アクセス

をご覧ください。 2. JavaScript のシングルトン モード

js では、よくグローバル変数をシングルトン モードとして使用します。例:

var a={};

なぜ a をグローバル変数として使用できるのか次の 2 つの条件を満たします:

1. オブジェクト a は一意である

2. A はグローバル スコープで定義され、グローバル アクセスを提供する

注: ただし、グローバル変数を減らすために、js で名前空間を使用することをお勧めします。

3. 遅延シングルトン

遅延シングルトン: 必要な場合にのみ作成されるオブジェクト インスタンス

目的: ページ上に 2 つのボタンがあり、クリックされると、応答ポップアップ ウィンドウが表示され、対応する 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>创建唯一的窗口</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 スキルのための 2 つのソリューション

NodeJS シングルトン モード、アダプター モード、デコレーション モード、オブザーバー モードの概要

JSシングルトンパターン詳細例_基礎知識

以上がjs シングルトン モードでポップアップ ウィンドウ インスタンス共有を作成するの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。