Skip to content

Instantly share code, notes, and snippets.

@jeremyroman
Created July 6, 2011 14:31
Show Gist options
  • Save jeremyroman/1067360 to your computer and use it in GitHub Desktop.
Save jeremyroman/1067360 to your computer and use it in GitHub Desktop.
Example of CommonJS modules
var nextNumber = 0;
module.exports.getNumber = function() {
nextNumber += 1;
return nextNumber;
};
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.
@kmiyashiro
Copy link

I think you mean myModule.getNumber();

@jeremyroman
Copy link
Author

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