Skip to content

Instantly share code, notes, and snippets.

@JamieMason
Created March 1, 2011 08:27
Show Gist options
  • Save JamieMason/848818 to your computer and use it in GitHub Desktop.
Save JamieMason/848818 to your computer and use it in GitHub Desktop.
JavaScript Singleton with lazy instantiation.
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