Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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;

This comment has been minimized.

Copy link

@tobiashochguertel tobiashochguertel commented May 29, 2018

Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.