Skip to content

Instantly share code, notes, and snippets.

@barmatz
Created December 10, 2014 12:12
Show Gist options
  • Select an option

  • Save barmatz/e19adfd5d9ac73fa8b94 to your computer and use it in GitHub Desktop.

Select an option

Save barmatz/e19adfd5d9ac73fa8b94 to your computer and use it in GitHub Desktop.
Node.js Singleton pattern
'use strict';
var singletonLock = {},
instance;
function MySingleton(lock) {
if (!(lock instanceof singletonLock)) {
throw new Error('This is a singleton, do not initiate!');
}
}
MySingleton.getInstance = function () {
return instance || (instance = new MySingleton(singletonLock));
};
module.exports = MySingleton;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment