Created
October 17, 2011 17:12
-
-
Save klovadis/1293127 to your computer and use it in GitHub Desktop.
Private class methods using the commonjs module system
This file contains hidden or 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
// ----- 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