Skip to content

Instantly share code, notes, and snippets.

@martin-sweeny
Last active July 14, 2016 17:43
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 martin-sweeny/3754cb5786e8354efa0081cdeb1e62ce to your computer and use it in GitHub Desktop.
Save martin-sweeny/3754cb5786e8354efa0081cdeb1e62ce to your computer and use it in GitHub Desktop.
/**
* Example Singleton constructor.
* I wish this was extendable but you gotta copy/paste the 2 lines into any object you want to be a Singleton
*
* @returns {*}
* @constructor
*/
function Singleton () {
if ( arguments.callee._singletonInstance ) return arguments.callee._singletonInstance;
arguments.callee._singletonInstance = this;
}
var Thing1 = new Singleton();
var Thing2 = new Singleton();
Thing1.foo = 'bar';
Thing2.foo; // 'bar'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment