Skip to content

Instantly share code, notes, and snippets.

@nahidakbar
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nahidakbar/4dd4e3ae2eb40954f0f6 to your computer and use it in GitHub Desktop.
Save nahidakbar/4dd4e3ae2eb40954f0f6 to your computer and use it in GitHub Desktop.
Options/Software/Singleton/singleton.js
/**
* @author Nahid Akbar
* @date 19/10/14
*/
"use strict";
var singleton = function(BaseClass, instanceParent, instanceVarName)
{
instanceParent = instanceParent !== undefined ? instanceParent : BaseClass;
instanceVarName = instanceVarName !== undefined ? instanceVarName : 'instance';
return function()
{
if (instanceParent[instanceVarName] === undefined)
{
var WithArguments = Function.prototype.bind.apply(BaseClass, Array.prototype.concat.apply([ null ], arguments));
instanceParent[instanceVarName] = new WithArguments();
}
return instanceParent[instanceVarName];
};
};
module.exports = singleton;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment