zoukankan      html  css  js  c++  java
  • 单例模式

    解决代码开销,提高代码效率

    var Model=function(id,html,open){
            this.id=id;
            this.html=html;
            this.open=false;
        }
        Model.prototype.create = function(){
            if(!this.open){       
                var model=document.createElement('div');
                model.innerHTML = this.html;
                model.id = this.id;
                document.body.appendChild(model);
                this.open = true;
                model.classList.remove('hide')          
                model.classList.add('show')   
            } 
        }
        Model.prototype.delete = function(){
            if(this.open){
                var model=document.getElementById('model');   
                document.body.removeChild(model);
                model.classList.add('hide') 
                this.open = false;
            }   
        }
        var createIntance = (function(){
            var instance;
            return function (){
                return instance || (instance = new Model('model','我是一个弹框')) //单例模式的核心,如果存在,则用存在的,不存在,则创建
            }
        })();
        var operate = {
            setModel:null,
            open:function(){
                this.setModel = createIntance();
                this.setModel.create();
            },
            delete:function(){
                this.setModel ? this.setModel.delete():'';
            }
        }
        document.getElementById("open").onclick=function(){
            operate.open();
        }
        document.getElementById("delete").onclick=function(){
            operate.delete();
        }
  • 相关阅读:
    Transformer详解
    PAT 1012
    PAT 1011
    PAT 1010
    Jordan Lecture Note-3: 梯度投影法
    PAT 1009
    PAT 1008
    Jordan Lecture Note-2: Maximal Margin Classifier
    PAT 1007
    PAT 1006
  • 原文地址:https://www.cnblogs.com/wxyblog/p/12612231.html
Copyright © 2011-2022 走看看