Created
July 6, 2011 14:31
-
-
Save jeremyroman/1067360 to your computer and use it in GitHub Desktop.
Example of CommonJS modules
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 nextNumber = 0; | |
module.exports.getNumber = function() { | |
nextNumber += 1; | |
return nextNumber; | |
}; |
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 mymodule = require('./mymodule'); | |
// mymodule in this file is now the module.exports object from mymodule.js | |
mymodule.getNumber(); // returns 1 | |
mymodule.getNumber(); // returns 2 | |
// There is no way to read or affect the local variable nextNumber | |
// except through the getNumber function exposed by mymodule. |
Yeah, sorry; I mentioned this in IRC as well. Thanks for pointing it out.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think you mean myModule.getNumber();