Last active
July 26, 2016 21:32
-
-
Save peteroid/87f45e86ec8b65f8947ad9d9395d0176 to your computer and use it in GitHub Desktop.
Node JS Simple Singleton used by require()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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