Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Happy-Ferret/0bb6c7a2ebb6ed94dbb11b5cb9a0aa85 to your computer and use it in GitHub Desktop.
Save Happy-Ferret/0bb6c7a2ebb6ed94dbb11b5cb9a0aa85 to your computer and use it in GitHub Desktop.
Private class methods using the commonjs module system
// ----- throw your class into one single file = a seperate scope
// constructor and export
var myClass = module.exports = function () {
// ..
}
// a public method
myClass.prototype.publicMethod = function () {
// can have access to the private method
privateMethod.apply(this);
}
// a private method
var privateMethod = function () {}
// ------ access the class from another file
var myClass = require('theabove.js');
// create new instance
var instance = new myClass();
// this will work
instance.publicMethod();
// this will fail
instance.privateMethod();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment