-
-
Save itsananderson/00af981d19aac1fab971 to your computer and use it in GitHub Desktop.
An illustration of module export behavior in Node
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
module.exports = Math.random(); |
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
module.exports = function() { | |
return Math.random(); | |
} |
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
// 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