Guest User

Untitled

a guest
Aug 16th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. /*
  2. Node wraps every .js file in a module function, which creates file-level scope. In contrast, all files are part of global scope (window object) for js in browser. To use functions from one module in another, it is necessary to export/import.
  3.  
  4. A property of the wrapper function is module.exports. It exists as an empty object if no exports have been defined. Exporting is is as simple as adding functions/vars to the module.exports object with module.exports = function or var. To import, use the requires(<path to export module>) method.
  5.  
  6. In the code below, function doesSomething is exported from file1.js and imported in file2.js
  7. */
  8.  
  9. // EXPORT
  10. // file1.js
  11. function doesSomething() {
  12. // block of code
  13. return something;
  14. }
  15.  
  16. // style 1
  17. module.exports.doesSomething = doesSomething; // exports entire module.exports object
  18.  
  19. // style 2
  20. module.exports = doesSomething; // exports only the function
  21.  
  22. // IMPORT
  23. // file2.js
  24. const importedFunc = require('relative-path-to-module'); // if in same dir, (./module). Note file extension absent.
Add Comment
Please, Sign In to add comment