Quantcast
Channel: Singleton Pattern in Javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 3

Singleton Pattern in Javascript

0
0

I'm reading Javascript Patterns recently.And I don't understand the code below when it talks about singleton pattern:

function Universe(){
    var instance;
    Universe=function Universe(){
        return instance;
    };
    Universe.prototype=this;

    //the new Universe below,refers to which one?The original one,
    //or the one:function(){return this;} ??
    instance=new Universe();

    instance.constructor=Universe;

    instance.bang="Big";

    return instance;
}
Universe.prototype.nothing=true;
var uni=new Universe();
Universe.prototype.everything=true;
var uni2=new Universe();

uni===uni2;//true

Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images