Skip to content

Instantly share code, notes, and snippets.

@ValeriiVasin
Created January 3, 2012 19:04
Show Gist options
  • Select an option

  • Save ValeriiVasin/1556360 to your computer and use it in GitHub Desktop.

Select an option

Save ValeriiVasin/1556360 to your computer and use it in GitHub Desktop.
[javascript patterns] Singleton.
// first realization: self-defining function
var Universe = function () {
var instance;
Universe = function () {
return instance;
};
Universe.prototype = this;
instance = new Universe();
instance.constructor = Universe;
// other functionality below...
instance.start = 0;
instance.finish = 100;
return instance;
};
// second realization: immediate function
var Universe;
(function () {
var instance;
Universe = function () {
if (instance) {
return instance;
}
instance = this;
this.start = 0;
this.finish = 100;
};
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment