Last active
February 18, 2017 20:46
-
-
Save tamg/7cb367d7c1fbaae866dcfc434abe397f to your computer and use it in GitHub Desktop.
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
//greet.js | |
var greet = function() { | |
console.log('Hello!'); | |
}; | |
module.exports = greet; | |
//app.js | |
var greet = require('./greet'); | |
greet(); | |
/** | |
- The require function takes the path of the greet module as a parameter | |
- The greet function is then wrapped inside another function | |
- Wrapping our module code protects variables and functions defined inside our module | |
- Thus module.exports becomes the way in which we expose our module | |
The newly wrapped greet module then becomes... | |
(function (exports, require, module, __filename, __dirname) { | |
var greet = function() { | |
console.log('Hello!'); | |
}; | |
module.exports = greet; | |
}); | |
- Then Node invokes the wrapped function by passing 4 parameters | |
fn(module.exports, require, moule, filename, dirname); | |
- Then require returns module.exports (which we have set inside our module file) | |
**/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment