JavaScript modules
organized and re-usable code (the "set phrases")
Organizing JavaScript code into reusable modules allows for:
namespace cleanliness: variable names do not get reused and cause confusion
instead of calling doStuff, you would call moduleX.doStuff and ensure that moduleX is unique to the program
functional focus: have one piece of code do one thing and one thing well!
this also makes the code more portable and shareable
we don't even have to know how they work internally
we just care that they do what we want them to do
Newer way of doing modules (used in React)
moduleToExport.js
moduleThatImports.js
Note that we can shorten our code slightly by rearranging the export
syntax:
moduleToExport.js
Older way of doing modules
moduleToExport.js
moduleThatImports.js
Last updated