Skip to content

Instantly share code, notes, and snippets.

@bttmly
Created August 28, 2014 22:41
Show Gist options
  • Save bttmly/c94464d27aa36aaf87b9 to your computer and use it in GitHub Desktop.
Save bttmly/c94464d27aa36aaf87b9 to your computer and use it in GitHub Desktop.
Singleton
var Singleton = (function() {
var instance;
return function () {
if ( !this instanceof Singleton ) {
return new Singleton();
}
if ( !instance ) {
instance = this;
}
return instance;
};
})();
var a = Singleton();
var b = new Singleton();
console.assert( a === b );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment