Skip to content

Instantly share code, notes, and snippets.

@peteroid
Last active July 26, 2016 21:32
Show Gist options
  • Save peteroid/87f45e86ec8b65f8947ad9d9395d0176 to your computer and use it in GitHub Desktop.
Save peteroid/87f45e86ec8b65f8947ad9d9395d0176 to your computer and use it in GitHub Desktop.
Node JS Simple Singleton used by require()
var singleton = function () {
// all are public
var someVariable = "some value" // acts like states
var otherVariable = 123 // acts like states
return {
someFunction: function (someArg) {
// expressions go here, change state
return "other value"
},
otherFunction: function () {
var someValue = this.someFunction() // must call with this to get into the context
otherVariable = (someValue + someVariable).length // for the variables, let the closures rock you!
}
}
}() // *** this is important
module.exports = singleton
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment