Skip to content

Instantly share code, notes, and snippets.

@dmhts
Created March 5, 2014 21:02
Show Gist options
  • Save dmhts/9376504 to your computer and use it in GitHub Desktop.
Save dmhts/9376504 to your computer and use it in GitHub Desktop.
Bulletproof singleton
/**
* Bulletproof singletone constructor. Can be used with or
* without new, can be called from whatever (object method,
* standalone function). Works fine in strict mode
*/
var Singletone = (function () {
var instance;
return function Construct_singletone () {
if (instance) {
return instance;
}
if (this && this.constructor === Construct_singletone) {
instance = this;
} else {
return new Construct_singletone();
}
};
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment