Created
March 1, 2011 08:27
-
-
Save JamieMason/848818 to your computer and use it in GitHub Desktop.
JavaScript Singleton with lazy instantiation.
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
var ConstructorName = (function() | |
{ | |
var singletonInstance = null; | |
function ConstructorName() | |
{ | |
// presumably expensive instantiation/init code | |
} | |
return function () | |
{ | |
if (singletonInstance === null) | |
{ | |
singletonInstance = new ConstructorName(); | |
} | |
return singletonInstance; | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment