Skip to content

Instantly share code, notes, and snippets.

@klovadis
Created October 17, 2011 17:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save klovadis/1293127 to your computer and use it in GitHub Desktop.
Save klovadis/1293127 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