Skip to content

Instantly share code, notes, and snippets.

@murdockcrc
Created July 29, 2015 18:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save murdockcrc/e5ff41e5fdc17e7dd36d to your computer and use it in GitHub Desktop.
Save murdockcrc/e5ff41e5fdc17e7dd36d to your computer and use it in GitHub Desktop.
NodeJS Singleton pattern
exports = module.exports = (function () {
// Instance stores a reference to the Singleton
var instance;
function init() {
// Singleton
// Private methods and variables
return {
// Public methods and variables
extend: function extend(extension, obj){
for (var key in extension){
obj[key] = extension[key];
}
}
};
};
return {
// Get the Singleton instance if one exists
// or create one if it doesn't
getInstance: function () {
if (!instance) {
instance = init();
}
return instance;
}
};
})();
@tobiashochguertel
Copy link

Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment