Skip to content

Instantly share code, notes, and snippets.

Embed
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;
}
};
})();
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.