Created
March 5, 2014 21:02
-
-
Save dmhts/9376504 to your computer and use it in GitHub Desktop.
Bulletproof singleton
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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