Modules Flashcards
What are four reasons we might use modules to contain JavaScript code?
- Allows us to find, fix, and debug more easily
- Allows us to reuse and recycle logic in different areas of the application.
- Keep information private and protected from other modules
- Avoid polluting the global namespace and potential naming collisions.
What the does process.argv[n] method return?
process.argv is an array of commands passed into the node environment. process.argv[n] returns the nth argument in the command (Zero-based indexing)
describe two ways of exporting a function from a module
- By defining a function, then passing that function definition via module.exports.functionName = functionName
- By setting module.exports.functionName to a function defined in the export dialogue. module.exports.functionName = function(name){code}
How would you import a function from a module after it has been exported?
You can use the require() function in the receiving file to import a function from a module. The require() function accepts a file path as a string.
require(‘./modules/moduleName.js’ would import all exported functions from moduleName.js.
You will then assign the functions via const functionName = moduleName.functionName
What does module.exports() create?
module.exports() creates an object that contains all exported functions from the moduleName.js file.
Why would we use Object Destructuring to import from a module?
Object Destructuring allows us to import only select functions from a module when not all of the exported functions are needed:
const { desiredFunctionName } = require(‘./modules/moduleName.js’)
How do we use object deconstruction to import a function from a module file?
We would write:
const { desiredFunctionName } = required(‘filePath/to/module.js’)
How does module importation/exportation differ between node and browser runtime environment?
Node uses module.exports and require(‘filePath) syntax
Browsers use export and import syntax
export {exportA, exportB } and import {exportA, exportB} from ‘filePath’
How do we export and import module files in a browser based runtime environment?
export {moduleExport} or export const moduleName = function
import {moduleExport} from ‘filePathName’
we also need to include type=’module’ to the script import to html
What do we need to remember when importing modules into HTML via the script tags?
We need to include type=”module” in addition to the filepath for the script
What might we do to avoid naming conflicts when importing a module?
We might use the import … as … syntax to avoid naming conflicts
import {greet as greetFrench } from ‘filePathName’
How do we use the default export to export the entire module file and all contained functions?
We first define the module code as an object by:
const resources = { codeBlockHere }
and then export {resources as default }
this will export the entire module file
How do we import a module exported as default?
We use:
import importedResources from ‘filePathName’
note that this is actually import {resources as importedResources} from ‘filePathName’
What is different in the import syntax of a browser runtime environment when we are importing a default export?
The curly braces are gone from around the imported moduleName
How do we create individual function variables having used a default export?
We import the default as normal and then deconstruct the default like so:
const {functionOne, functionTwo } = defaultName