Skip to content

Instantly share code, notes, and snippets.

@itsananderson
Created October 12, 2014 16:18
Show Gist options
  • Save itsananderson/00af981d19aac1fab971 to your computer and use it in GitHub Desktop.
Save itsananderson/00af981d19aac1fab971 to your computer and use it in GitHub Desktop.
An illustration of module export behavior in Node
module.exports = Math.random();
module.exports = function() {
return Math.random();
}
// If you require a module multiple times, you'll always get the same result
console.log(require('./a'));
console.log(require('./a'));
console.log(require('./a'));
console.log(require('./a'));
console.log(require('./a'));
console.log(require('./a'));
// If you want distinct instances, the module can return a factory/constructor
var rand = require('./b');
console.log(rand());
console.log(rand());
console.log(rand());
console.log(rand());
console.log(rand());
console.log(rand());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment