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

    //结合闭包、原型
    (function(){
    function Person(){

    }
    Person.prototype.id = 12;
    Person.prototype.age = 23;
    Person.prototype.name = "李项京";
    Person.prototype.method = function(){
    return "sdf";
    };
    Person.prototype["person"] =new Person();
    function getInstance(){
    return Person.prototype.person;
    }
    window.getInstance = getInstance;
    })(window);
    alert(window.getInstance().name); //三次调用,只执行一次new Person,说明已经是单例了
    alert(window.getInstance().id);
    alert(window.getInstance().age);
    alert(window.getInstance().method());
  • 相关阅读:
    numpy
    shell进阶2
    python笔记3
    shell进阶1
    记录:Paxos原理、历程及实战
    收藏
    linux fs io
    linux mount 操作
    docker好文收藏
    rbd snap(1)
  • 原文地址:https://www.cnblogs.com/kuyuyingzi/p/4266233.html
Copyright © 2011-2022 走看看